All Products
Search
Document Center

Object Storage Service:Configure single-connection bandwidth throttling in PHP

Last Updated:Mar 08, 2024

This topic describes how to add parameters in an object upload or download request to set the limit of upload or download bandwidth. This ensures sufficient bandwidth for other applications.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

Configure single-connection bandwidth throttling for simple upload and download

The following sample code provides an example on how to configure single-connection bandwidth throttling for simple upload and download:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
$object = "exampledir/exampleobject.txt";
// Specify the content of the object. 
$content = "hello world";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

// Set the bandwidth limit to 100 KB/s, which is equal to 819,200 bit/s. 
$options = array(
      OssClient::OSS_HEADERS => array(
              OssClient::OSS_TRAFFIC_LIMIT => 819200,
));

try {
    // Configure bandwidth throttling for object upload. 
    $ossClient->putObject($bucket, $object, $content, $options);

    // Configure bandwidth throttling for object download. 
    $ossClient->getObject($bucket, $object, $options);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");

Configure single-connection bandwidth throttling for multipart upload

The following code provides an example on how to configure single-connection bandwidth throttling for multipart upload:

<?php
  if (is_file(__DIR__ . '/../autoload.php')) {
  require_once __DIR__ . '/../autoload.php';
  }
  if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  require_once __DIR__ . '/../vendor/autoload.php';
  }
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
$object = "exampledir/exampleobject.txt";
// Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. By default, if you do not specify the path of the local file, the file is uploaded from the path of the project to which the sample program belongs. 
$uploadFile = "D:\\localpath\\examplefile.txt";

/**
* Step 1: Initiate a Multipart upload task and obtain the upload ID. 
*/
try{
  $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

  // Obtain the upload ID. An upload ID is the unique identifier for a multipart upload task. You can use an upload ID to perform related operations such as canceling or querying the multipart upload task. 
  $uploadId = $ossClient->initiateMultipartUpload($bucket, $object);
} catch(OssException $e) {
  printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
  printf($e->getMessage() . "\n");
  return;
}
print(__FUNCTION__ . ": initiateMultipartUpload OK" . "\n");
/*
* Step 2: Upload parts. 
*/
$partSize = 10 * 1024 * 1024;
$uploadFileSize = filesize($uploadFile);
$pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
$responseUploadPart = array();
$uploadPosition = 0;
$isCheckMd5 = true;
foreach ($pieces as $i => $piece) {
  $fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
  $toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
  $upOptions = array(
    // Upload the object. 
    $ossClient::OSS_FILE_UPLOAD => $uploadFile,
    // Specify part numbers. 
    $ossClient::OSS_PART_NUM => ($i + 1),
    // Specify the position from which the multipart upload task starts. 
    $ossClient::OSS_SEEK_TO => $fromPos,
    // Specify the object length. 
    $ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
    // Specify whether to enable MD5 verification. A value of true indicates that MD5 verification is enabled. 
    $ossClient::OSS_CHECK_MD5 => $isCheckMd5,
    // Set the bandwidth limit to 100 KB/s, which is equal to 819,200 bit/s. 
    $options = array(
      OssClient::OSS_HEADERS => array(
        OssClient::OSS_TRAFFIC_LIMIT => 819200,
      ))

  );
  // Enable MD5 verification. 
  if ($isCheckMd5) {
    $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
    $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
  }
  try {
    // Upload the parts. 
    $responseUploadPart[] = $ossClient->uploadPart($bucket, $object, $uploadId, $upOptions);
  } catch(OssException $e) {
    printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
    printf($e->getMessage() . "\n");
    return;
  }
  printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n");
}
// The $uploadParts parameter is an array that consists of the ETag and PartNumber parameters of each part. 
$uploadParts = array();
foreach ($responseUploadPart as $i => $eTag) {
  $uploadParts[] = array(
    'PartNumber' => ($i + 1),
    'ETag' => $eTag,
  );
}
/**
* Step 3: Complete the multipart upload task. 
*/
try {
  // All valid values of the $uploadParts parameter are required for the CompleteMultipartUpload operation. After OSS receives the values of the $uploadParts parameter, OSS verifies all parts one by one. After the verification of all parts is completed, OSS combines these parts into a complete object. 
  $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
  }  catch(OssException $e) {
  printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
  printf($e->getMessage() . "\n");
  return;
  }
  printf(__FUNCTION__ . ": completeMultipartUpload OK\n");

Configure bandwidth throttling for upload and download that use signed URLs

The following sample code provides an example on how to configure single-connection bandwidth throttling when you use signed URLs to upload or download objects:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
$object = "exampledir/exampleobject.txt";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

// Set the bandwidth limit to 100 KB/s, which is equal to 819,200 bit/s. 
$options = array(
        OssClient::OSS_TRAFFIC_LIMIT => 819200,
);

// Generate a signed URL for object upload with single-connection bandwidth throttling configured and set the validity period of the URL to 60 seconds. 
$timeout = 60;
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);
print($signedUrl);

// Generate a signed URL for object download with single-connection bandwidth throttling configured and set the validity period of the URL to 120 seconds. 
$timeout = 120;
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print($signedUrl);