This topic describes how to ensure other applications have sufficient bandwidth when you perform upload and download operations on OSS. You can include the x-oss-traffic-limit parameter in your requests and set bandwidth limits.

Note For more information about the scenarios and precautions of the single-connection bandwidth throttling feature, see Single-connection bandwidth throttling in the OSS Developer Guide.

Configure bandwidth throttling for common uploads and downloads

Use the following code to configure bandwidth throttling when you upload and download objects to and from OSS:

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

import oss2
from oss2.models import OSS_TRAFFIC_LIMIT

# Security risks may arise if you log on with the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations in OSS. We recommend that you log on with a RAM user account to call API operations or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses China (Hangzhou) as the endpoint. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourRequestBucketName>')

object_name = '<yourobjectName>'
local_file_name = '<yourLocalFileName>'
down_file_name = '<yourDownloadFileName>'

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

# Configure bandwidth throttling for the object to be uploaded.
result = bucket.put_object_from_file(object_name, local_file_name, headers=headers)
print('http response status:', result.status)

# Configure bandwidth throttling for the object to be downloaded.
result = bucket.get_object_to_file(object_name, down_file_name, headers=headers)
print('http response status:', result.status)

Configure bandwidth throttling for uploads and downloads that use signed URLs

Use the following code to configure bandwidth throttling when you upload and download objects from and to OSS by using signed URLs.

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

import oss2
from oss2.models import OSS_TRAFFIC_LIMIT

# Security risks may arise if you log on with the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations in OSS. We recommend that you log on with a RAM user account to call API operations or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses China (Hangzhou) as the endpoint. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourRequestBucketName>')

object_name = '<yourobjectName>'
local_file_name = '<yourLocalFileName>'
down_file_name = '<yourDownloadFileName>'

# Set the bandwidth limit to 100 KB/s (819200 bit/s) in request parameters.
limit_speed = (100 * 1024 * 8)
params = dict()
params[OSS_TRAFFIC_LIMIT] = str(limit_speed);

# Create a signed URL for uploading an object and set the valid period of the URL to 60s.
url = bucket.sign_url('PUT', object_name, 60, params=params)
print('put object url:', url)

# Configure bandwidth throttling for the object to be uploaded.
result = bucket.put_object_with_url_from_file(url, local_file_name)
print('http response status:', result.status)

# Create a signed URL for downloading an object and set the valid period of the URL to 60s.
url = bucket.sign_url('GET', object_name, 60, params=params)
print('get object url:', url)

# Configure bandwidth throttling for the object to be downloaded.
result = bucket.get_object_with_url_to_file(url, down_file_name)
print('http response status:', result.status)