This topic describes how to add parameters in an object upload and download request to set the limit of upload or download bandwidth. This way, you can ensure other applications have sufficient bandwidth.

Configure single-connection bandwidth throttling for simple upload and download

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

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

import oss2
from oss2.models import OSS_TRAFFIC_LIMIT

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to 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. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Specify the full path of the object. The path cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
object_name = '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 local file, the local file is uploaded from the path of the project to which the sample program belongs. 
local_file_name = 'D:\\localpath\\examplefile.txt'
# Specify the full path of the local file to which you want the object to be downloaded. If the specified local file exists, the object to download replaces the file. Otherwise, a file is created. 
# If the path for the object is not specified, the downloaded object is saved to the path of the project to which the sample program belongs. 
down_file_name = 'D:\\localpath\\exampleobject.txt'

# Set the related headers to limit bandwidth throttling to 100 KB/s, which is equal to 819200 bit/s. 
limit_speed = (100 * 1024 * 8)
headers = dict()
headers[OSS_TRAFFIC_LIMIT] = str(limit_speed);

# Configure single-connection bandwidth throttling for the object upload. 
result = bucket.put_object_from_file(object_name, local_file_name, headers=headers)
print('http response status:', result.status)

# Configure single-connection bandwidth throttling for the object download. 
result = bucket.get_object_to_file(object_name, down_file_name, headers=headers)
print('http response status:', result.status)

Configure single-connection bandwidth throttling for object uploads and downloads that use signed URLs

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

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

import oss2
from oss2.models import OSS_TRAFFIC_LIMIT

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to 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. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Specify the full path of the object. The path cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
object_name = '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 local file, the local file is uploaded from the path of the project to which the sample program belongs. 
local_file_name = 'D:\\localpath\\examplefile.txt'
# Specify the full path of the local file to which you want the object to be downloaded. If the specified local file exists, the object to download replaces the file. Otherwise, a file is created. 
# If the path for the object is not specified, the downloaded object is saved to the path of the project to which the sample program belongs. 
down_file_name = 'D:\\localpath\\exampleobject.txt'

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

# Generate a signed URL used to upload an object with single-connection bandwidth throttling configured and set the validity period of the URL to 60 seconds. 
url = bucket.sign_url('PUT', object_name, 60, params=params)
print('put object url:', url)

# Configure single-connection bandwidth throttling for the object upload. 
result = bucket.put_object_with_url_from_file(url, local_file_name)
print('http response status:', result.status)

# Generate a signed URL used to download an object with single-connection bandwidth throttling configured and set the validity period of the URL to 60 seconds. 
url = bucket.sign_url('GET', object_name, 60, params=params)
print('get object url:', url)

# Configure single-connection bandwidth throttling for the object download. 
result = bucket.get_object_with_url_to_file(url, down_file_name)
print('http response status:', result.status)

References

For more information about the complete sample code for single-connection bandwidth throttling, visit GitHub.