All Products
Search
Document Center

Object Storage Service:Single-connection bandwidth throttling

Last Updated:Mar 20, 2026

OSS lets you set a per-request bandwidth cap on uploads and downloads. Use this to prevent a single connection from saturating the network and starving other applications.

How it works

Set the x-oss-traffic-limit header on any supported request. OSS enforces the limit for the duration of that connection only — other connections are unaffected.

Supported operations: PutObject, AppendObject, PostObject, CopyObject, UploadPart, UploadPartCopy, GetObject

Parameter constraints:

ParameterUnitMinMax
x-oss-traffic-limitbit/s819,200 (100 KB/s)838,860,800 (100 MB/s)

Choose a throttling method

MethodWhen to useAccess type
SDK request headerProgrammatic uploads and downloadsAny (public or private)
File URL parameterBrowser or direct URL accesspublic-read or public-read-write only
Signed URL parameterTemporary access to private objectsPrivate

Throttle SDK requests

Set the throttle value on the request object before calling the upload or download method. All examples below set the limit to 100 KB/s (819,200 bit/s).

For examples in other languages, see SDK overview.

Simple upload and download

Java

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 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.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
        String objectName = "exampledir/exampleobject.txt";
        // Specify the full path of the local file to upload. Example: D:\\localpath\\examplefile.txt.
        String localFileName = "D:\\localpath\\examplefile.txt";
        // Specify the local path to download the object to.
        String downLoadFileName = "D:\\localpath\\exampleobject.txt";

        // Set the bandwidth limit to 100 KB/s.
        int limitSpeed = 100 * 1024 * 8;
        String region = "cn-hangzhou";

        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
            .endpoint(endpoint)
            .credentialsProvider(credentialsProvider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)
            .build();

        try {
            // Upload with bandwidth throttling.
            InputStream inputStream = new FileInputStream(localFileName);
            PutObjectRequest PutObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            PutObjectRequest.setTrafficLimit(limitSpeed);
            ossClient.putObject(PutObjectRequest);

            // Download with bandwidth throttling.
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
            getObjectRequest.setTrafficLimit(limitSpeed);
            File localFile = new File(downLoadFileName);
            ossClient.getObject(getObjectRequest, localFile);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

PHP

<?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. Example: https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
$bucket= "examplebucket";
$object = "exampledir/exampleobject.txt";
$content = "hello world";

$config = array(
    "provider" => $provider,
    "endpoint" => $endpoint,
    "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
    "region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);

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

try {
    // Upload with bandwidth throttling.
    $ossClient->putObject($bucket, $object, $content, $options);

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

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

Node.js

When bandwidth throttling is active, transfers take longer than usual. Increase the SDK timeout beyond the default 60,000 ms to avoid timeout errors on large objects.
const OSS = require('ali-oss')

const client = new OSS({
  region: 'yourregion',
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  bucket: 'yourbucketname'
});

// Set the bandwidth limit to 100 KB/s.
const headers = {
  'x-oss-traffic-limit': 8 * 1024 * 100
}

// Upload with bandwidth throttling.
async function put() {
  const filePath = 'D:\\localpath\\examplefile.txt';
  const fileStream = fs.createReadStream(filePath);
  const result = await client.putStream('file-name', fileStream, {
    headers,
    // Increase timeout when throttling is active.
    timeout: 60000
  });
  console.log(result);
}

put()

// Download with bandwidth throttling.
async function get() {
  const result = await client.get('file name', {
    headers,
    // Increase timeout when throttling is active.
    timeout: 60000
  })
  console.log(result)
}

get()

Python

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import OSS_TRAFFIC_LIMIT

# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"

bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

object_name = 'exampledir/exampleobject.txt'
local_file_name = 'D:\\localpath\\examplefile.txt'
down_file_name = 'D:\\localpath\\exampleobject.txt'

# Set the bandwidth limit to 100 KB/s (819,200 bit/s).
limit_speed = (100 * 1024 * 8)
headers = dict()
headers[OSS_TRAFFIC_LIMIT] = str(limit_speed)

# Upload with bandwidth throttling.
result = bucket.put_object_from_file(object_name, local_file_name, headers=headers)
print('http response status:', result.status)

# Download with bandwidth throttling.
result = bucket.get_object_to_file(object_name, down_file_name, headers=headers)
print('http response status:', result.status)

C#

using System.Text;
using Aliyun.OSS;
using Aliyun.OSS.Common;

var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
    byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
    MemoryStream requestContent = new MemoryStream(binaryData);
    // Set the bandwidth limit to 100 KB/s (819,200 bit/s).
    var putRequest = new PutObjectRequest(bucketName, objectName, requestContent)
    {
        TrafficLimit = 100*1024*8
    };
    client.PutObject(putRequest);
    Console.WriteLine("Put object succeeded");

    // Set the bandwidth limit to 100 KB/s (819,200 bit/s).
    var getRequest = new GetObjectRequest(bucketName, objectName)
    {
        TrafficLimit = 100 * 1024 * 8
    };
    var getResult = client.GetObject(getRequest);
    Console.WriteLine("Get object succeeded");
}
catch (Exception ex)
{
    Console.WriteLine("Put object failed, {0}", ex.Message);
}

Go

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// 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, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	localFilePath := "D:\\localpath\\examplefile.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Set the bandwidth limit to 5 MB/s (41,943,040 bit/s).
	var traffic int64 = 41943040

	objectName := "exampledir/exampleobject.txt"
	err = bucket.PutObject(objectName, fd, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFile(objectName, downloadFilePath, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

C++

#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;

int main(void)
{
    std::string Endpoint = "yourEndpoint";
    std::string Region = "yourRegion";
    std::string BucketName = "examplebucket";
    std::string ObjectName = "exampledir/exampleobject.txt";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Upload with bandwidth throttling. Set the limit to 100 KB/s (819,200 bit/s). */
    std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("yourLocalFilename", std::ios::in|std::ios::binary);
    PutObjectRequest putrequest(BucketName, ObjectName, content);
    putrequest.setTrafficLimit(819200);
    auto putoutcome = client.PutObject(putrequest);

    /* Download with bandwidth throttling. Set the limit to 100 KB/s (819,200 bit/s). */
    GetObjectRequest getrequest(BucketName, ObjectName);
    getrequest.setTrafficLimit(819200);
    auto getoutcome = client.GetObject(getrequest);

    ShutdownSdk();
    return 0;
}

Multipart upload

Apply the same x-oss-traffic-limit header to each UploadPart request.

Java

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class DemoApi2 {

    public static void main(String[] args) throws Exception {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        String bucketName = "examplebucket";
        String objectName = "exampledir/exampleobject.txt";
        // Set the bandwidth limit to 100 KB/s.
        int limitSpeed = 100 * 1024 * 8;
        String region = "cn-hangzhou";

        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
            .endpoint(endpoint)
            .credentialsProvider(credentialsProvider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)
            .build();

        try {
            // Initiate the multipart upload task.
            InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
            InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request);
            String uploadId = upresult.getUploadId();

            List<PartETag> partETags = new ArrayList<PartETag>();
            // Set each part to 1 MB.
            final long partSize = 1 * 1024 * 1024L;

            final File sampleFile = new File("D:\\localpath\\examplefile.txt");
            long fileLength = sampleFile.length();
            int partCount = (int) (fileLength / partSize);
            if (fileLength % partSize != 0) {
                partCount++;
            }

            // Upload parts with bandwidth throttling.
            for (int i = 0; i < partCount; i++) {
                long startPos = i * partSize;
                long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
                InputStream instream = new FileInputStream(sampleFile);
                instream.skip(startPos);
                UploadPartRequest uploadPartRequest = new UploadPartRequest();
                uploadPartRequest.setBucketName(bucketName);
                uploadPartRequest.setKey(objectName);
                uploadPartRequest.setUploadId(uploadId);
                uploadPartRequest.setInputStream(instream);
                // Each part except the last must be at least 100 KB.
                uploadPartRequest.setPartSize(curPartSize);
                // Part numbers range from 1 to 10000.
                uploadPartRequest.setPartNumber(i + 1);
                uploadPartRequest.setTrafficLimit(limitSpeed);
                UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                partETags.add(uploadPartResult.getPartETag());
            }

            // Complete the multipart upload.
            CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);
            CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            System.out.println(completeMultipartUploadResult.getETag());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

PHP

<?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;

$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "yourEndpoint";
$bucket= "examplebucket";
$object = "exampledir/exampleobject.txt";
$uploadFile = "D:\\localpath\\examplefile.txt";

/**
* Step 1: Initiate the multipart upload task.
*/
try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $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 with bandwidth throttling.
*/
$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(
        $ossClient::OSS_FILE_UPLOAD => $uploadFile,
        $ossClient::OSS_PART_NUM => ($i + 1),
        $ossClient::OSS_SEEK_TO => $fromPos,
        $ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
        $ossClient::OSS_CHECK_MD5 => $isCheckMd5,
        // Set the bandwidth limit to 100 KB/s (819,200 bit/s).
        $options = array(
            OssClient::OSS_HEADERS => array(
                OssClient::OSS_TRAFFIC_LIMIT => 819200,
            ))
    );
    if ($isCheckMd5) {
        $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
        $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
    }
    try {
        $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");
}
$uploadParts = array();
foreach ($responseUploadPart as $i => $eTag) {
    $uploadParts[] = array(
        'PartNumber' => ($i + 1),
        'ETag' => $eTag,
    );
}

/**
* Step 3: Complete the multipart upload task.
*/
try {
    $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
printf(__FUNCTION__ . ": completeMultipartUpload OK\n");

Python

# -*- coding: utf-8 -*-
import os
from oss2 import SizedFileAdapter, determine_part_size
from oss2.headers import OSS_TRAFFIC_LIMIT
from oss2.models import PartInfo
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"

bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

key = 'exampledir/exampleobject.txt'
filename = 'D:\\localpath\\examplefile.txt'

total_size = os.path.getsize(filename)
part_size = determine_part_size(total_size, preferred_size=100 * 1024)

# Initiate the multipart upload task.
upload_id = bucket.init_multipart_upload(key).upload_id
parts = []

# Set the bandwidth limit to 100 KB/s (819,200 bit/s).
limit_speed = (100 * 1024 * 8)
headers = dict()
headers[OSS_TRAFFIC_LIMIT] = str(limit_speed)

# Upload parts with bandwidth throttling.
with open(filename, 'rb') as fileobj:
    part_number = 1
    offset = 0
    while offset < total_size:
        num_to_upload = min(part_size, total_size - offset)
        result = bucket.upload_part(key, upload_id, part_number,
                                    SizedFileAdapter(fileobj, num_to_upload), headers=headers)
        parts.append(PartInfo(part_number, result.etag))

        offset += num_to_upload
        part_number += 1

# Complete the multipart upload task.
bucket.complete_multipart_upload(key, upload_id, parts)

# Verify the upload.
with open(filename, 'rb') as fileobj:
    assert bucket.get_object(key).read() == fileobj.read()

Go

package main

import (
	"io"
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Set the bandwidth limit to 5 MB/s (41,943,040 bit/s).
	traffic := int64(41943040)

	// Split the file into three parts for multipart upload.
	localFilePath := "localFile"
	chunks, err := oss.SplitFileByPartNum(localFilePath, 3)
	if err != nil {
		log.Fatalf("Failed to split file '%s': %v", localFilePath, err)
	}

	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	objectName := "exampledir/exampleobject.txt"
	imur, err := bucket.InitiateMultipartUpload(objectName)
	if err != nil {
		log.Fatalf("Failed to initiate multipart upload for '%s': %v", objectName, err)
	}

	// Upload parts with bandwidth throttling.
	var parts []oss.UploadPart
	for _, chunk := range chunks {
		_, err := fd.Seek(chunk.Offset, io.SeekStart)
		if err != nil {
			log.Fatalf("Failed to seek to offset %d in file '%s': %v", chunk.Offset, localFilePath, err)
		}
		part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, oss.TrafficLimitHeader(traffic))
		if err != nil {
			log.Fatalf("Failed to upload part %d of '%s': %v", chunk.Number, objectName, err)
		}
		parts = append(parts, part)
	}

	_, err = bucket.CompleteMultipartUpload(imur, parts)
	if err != nil {
		log.Fatalf("Failed to complete multipart upload for '%s': %v", objectName, err)
	}

	log.Println("Multipart upload completed successfully")
}

Throttle using a file URL

For objects with public-read or public-read-write ACL, append x-oss-traffic-limit=<value> to the object URL.

Example: The following URL throttles downloads of video.mp4 to 100 KB/s:

https://examplebucket.oss-cn-hangzhou.aliyuncs.com/video.mp4?x-oss-traffic-limit=819200

Throttle using a signed URL

For private objects, include x-oss-traffic-limit in the signature calculation when generating the signed URL. The parameter is embedded in the URL and enforced when the URL is used.

For examples in other languages, see SDK overview.

Java

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class Demo {
    public static void main(String[] args) throws Throwable {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        String bucketName = "examplebucket";
        String objectName = "exampledir/exampleobject.txt";
        String localFileName = "D:\\localpath\\examplefile.txt";
        String downLoadFileName = "D:\\localpath\\exampleobject.txt";

        // Set the bandwidth limit to 100 KB/s.
        int limitSpeed = 100 * 1024 * 8;
        String region = "cn-hangzhou";

        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
            .endpoint(endpoint)
            .credentialsProvider(credentialsProvider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)
            .build();

        try {
            // Generate a signed URL for upload, valid for 60 seconds.
            Date date = new Date();
            date.setTime(date.getTime() + 60 * 1000);
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
            request.setExpiration(date);
            request.setTrafficLimit(limitSpeed);
            URL signedUrl = ossClient.generatePresignedUrl(request);
            System.out.println("put object url" + signedUrl);

            // Upload using the signed URL.
            InputStream inputStream = new FileInputStream(localFileName);
            ossClient.putObject(signedUrl, inputStream, -1, null, true);

            // Generate a signed URL for download, valid for 60 seconds.
            date = new Date();
            date.setTime(date.getTime() + 60 * 1000);
            request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
            request.setExpiration(date);
            request.setTrafficLimit(limitSpeed);
            signedUrl = ossClient.generatePresignedUrl(request);
            System.out.println("get object url" + signedUrl);

            // Download using the signed URL.
            GetObjectRequest getObjectRequest = new GetObjectRequest(signedUrl, null);
            ossClient.getObject(getObjectRequest, new File(downLoadFileName));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

PHP

<?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;

$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "yourEndpoint";
$bucket= "examplebucket";
$object = "exampledir/exampleobject.txt";

$config = array(
    "provider" => $provider,
    "endpoint" => $endpoint,
    "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
    "region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);

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

// Generate a signed URL for upload, valid for 60 seconds.
$timeout = 60;
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);
print($signedUrl);

// Generate a signed URL for download, valid for 120 seconds.
$timeout = 120;
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print($signedUrl);

Python

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import OSS_TRAFFIC_LIMIT

auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"

bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

object_name = 'exampledir/exampleobject.txt'
local_file_name = 'D:\\localpath\\examplefile.txt'
down_file_name = 'D:\\localpath\\exampleobject.txt'

# Set the bandwidth limit to 100 KB/s (819,200 bit/s).
limit_speed = (100 * 1024 * 8)
params = dict()
params[OSS_TRAFFIC_LIMIT] = str(limit_speed)

# Generate a signed URL for upload, valid for 60 seconds.
url = bucket.sign_url('PUT', object_name, 60, params=params)
print('put object url:', url)

# Upload using the signed URL.
result = bucket.put_object_with_url_from_file(url, local_file_name)
print('http response status:', result.status)

# Generate a signed URL for download, valid for 60 seconds.
url = bucket.sign_url('GET', object_name, 60, params=params)
print('get object url:', url)

# Download using the signed URL.
result = bucket.get_object_with_url_to_file(url, down_file_name)
print('http response status:', result.status)

Go

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	localFilePath := "D:\\localpath\\exampleobject.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Set the bandwidth limit to 5 MB/s (41,943,040 bit/s).
	traffic := int64(41943040)

	objectName := "exampledir/exampleobject.txt"

	// Generate a signed URL for upload, valid for 60 seconds.
	strURL, err := bucket.SignURL(objectName, oss.HTTPPut, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for uploading '%s': %v", objectName, err)
	}

	err = bucket.PutObjectWithURL(strURL, fd)
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	// Generate a signed URL for download, valid for 60 seconds.
	strURL, err = bucket.SignURL(objectName, oss.HTTPGet, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for downloading '%s': %v", objectName, err)
	}

	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFileWithURL(strURL, downloadFilePath)
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

C#

using System.Text;
using Aliyun.OSS;
using Aliyun.OSS.Common;

var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
var downloadFilename  = "D:\\localpath\\examplefile.txt";
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
    // Generate a signed URL for upload with bandwidth throttling (valid for 1 hour).
    var generatePresignedUriRequest = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Put)
    {
        Expiration = DateTime.Now.AddHours(1),
    };

    // Set the bandwidth limit to 100 KB/s (819,200 bit/s).
    generatePresignedUriRequest.AddQueryParam("x-oss-traffic-limit", "819200");

    var signedUrl = client.GeneratePresignedUri(generatePresignedUriRequest);
    var buffer = Encoding.UTF8.GetBytes(objectContent);
    using (var ms = new MemoryStream(buffer))
    {
        client.PutObject(signedUrl, ms);
    }
    Console.WriteLine("Put object by signature succeeded. {0} ", signedUrl.ToString());

    var metadata = client.GetObjectMetadata(bucketName, objectName);
    var etag = metadata.ETag;

    // Generate a signed URL for download with bandwidth throttling.
    // Set the bandwidth limit to 100 KB/s (819,200 bit/s).
    var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get);
    req.AddQueryParam("x-oss-traffic-limit", "819200");

    var uri = client.GeneratePresignedUri(req);
    OssObject ossObject = client.GetObject(uri);
    using (var file = File.Open(downloadFilename, FileMode.OpenOrCreate))
    {
        using (Stream stream = ossObject.Content)
        {
            int length = 4 * 1024;
            var buf = new byte[length];
            do
            {
                length = stream.Read(buf, 0, length);
                file.Write(buf, 0, length);
            } while (length != 0);
        }
    }
    Console.WriteLine("Get object by signature succeeded. {0} ", uri.ToString());
}
catch (Exception ex)
{
    Console.WriteLine("Put object failed, {0}", ex.Message);
}

C++

#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
    std::string Endpoint = "yourEndpoint";
    std::string Region = "yourRegion";
    std::string BucketName = "examplebucket";
    std::string ObjectName = "exampledir/exampleobject.txt";

    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Generate a signed URL for upload, valid for 1,200 seconds. Set the limit to 100 KB/s (819,200 bit/s). */
    std::time_t expires = std::time(nullptr) + 1200;
    GeneratePresignedUrlRequest putrequest(BucketName, ObjectName, Http::Put);
    putrequest.setExpires(expires);
    putrequest.setTrafficLimit(819200);
    auto genOutcome = client.GeneratePresignedUrl(putrequest);
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";
    std::cout << "Signed upload URL:" << genOutcome.result() << std::endl;

    auto outcome = client.PutObjectByUrl(genOutcome.result(), content);

    /* Generate a signed URL for download, valid for 1,200 seconds. Set the limit to 100 KB/s (819,200 bit/s). */
    GeneratePresignedUrlRequest getrequest(BucketName, ObjectName, Http::Get);
    getrequest.setExpires(expires);
    getrequest.setTrafficLimit(819200);
    genOutcome = client.GeneratePresignedUrl(getrequest);
    std::cout << "Signed download URL:" << genOutcome.result() << std::endl;
    auto goutcome = client.GetObjectByUrl(genOutcome.result());

    ShutdownSdk();
    return 0;
}