All Products
Search
Document Center

Object Storage Service:Transfer acceleration (Python SDK V2)

Last Updated:Aug 01, 2025

Transfer acceleration improves the speed of accessing OSS for users across the globe. This feature is suitable for scenarios such as long-distance data transmission and uploading or downloading large files that are gigabytes or terabytes in size.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region ID (cn-hangzhou) and a public endpoint. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and their corresponding endpoints, see OSS regions and endpoints.

Method definitions

Enable transfer acceleration

put_bucket_transfer_acceleration(request: PutBucketTransferAccelerationRequest, **kwargs) → PutBucketTransferAccelerationResult

Query the transfer acceleration status

get_bucket_transfer_acceleration(request: GetBucketTransferAccelerationRequest, **kwargs) → GetBucketTransferAccelerationResult

Request parameters

Parameter

Type

Description

request

PutBucketTransferAccelerationRequest

The request parameters. For more information, see PutBucketTransferAccelerationRequest.

GetBucketTransferAccelerationRequest

The request parameters. For more information, see GetBucketTransferAccelerationRequest.

Return values

Type

Description

PutBucketTransferAccelerationResult

The return value. For more information, see PutBucketTransferAccelerationResult.

GetBucketTransferAccelerationResult

The return value. For more information, see GetBucketTransferAccelerationResult.

For more information about the method to enable transfer acceleration, see put_bucket_transfer_acceleration.

For more information about the method to query the transfer acceleration status, see get_bucket_transfer_acceleration.

Sample code

Enable transfer acceleration

The following code provides an example of how to enable the transfer acceleration feature for a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: set the transfer acceleration configuration for a bucket.
parser = argparse.ArgumentParser(description="put bucket transfer acceleration sample")

# Define command-line arguments, including the required region, bucket name, endpoint, and whether to enable transfer acceleration.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--enabled', help='Whether the transfer acceleration is enabled for this bucket. Default is False.', default=False, type=bool)

def main():
    # Parse the command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the preceding configurations to prepare for interaction with OSS.
    client = oss.Client(cfg)

    # Send a request to set the transfer acceleration configuration for the specified bucket.
    result = client.put_bucket_transfer_acceleration(oss.PutBucketTransferAccelerationRequest(
            bucket=args.bucket,  # The bucket name.
            transfer_acceleration_configuration=oss.TransferAccelerationConfiguration(
                enabled=args.enabled,  # Specifies whether to enable transfer acceleration.
            ),
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The script entry point, from which the program flow starts.

Query the transfer acceleration status

The following code provides an example of how to query the transfer acceleration status of a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: obtain the transfer acceleration configuration of a bucket.
parser = argparse.ArgumentParser(description="get bucket transfer acceleration sample")

# Define command-line arguments, including the required region, bucket name, and endpoint.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse the command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the preceding configurations to prepare for interaction with OSS.
    client = oss.Client(cfg)

    # Send a request to obtain the details of the transfer acceleration configuration for the specified bucket.
    result = client.get_bucket_transfer_acceleration(oss.GetBucketTransferAccelerationRequest(
            bucket=args.bucket,  # The bucket name.
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' enabled: {result.transfer_acceleration_configuration.enabled if hasattr(result.transfer_acceleration_configuration, "enabled") else "Not set"},'  # Specifies whether transfer acceleration is enabled. If it is not set, "Not set" is displayed.
          )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The script entry point, from which the program flow starts.

References