Todos os produtos
Search
Central de documentação

Object Storage Service:Aceleração de transferência (Python SDK V2)

Última atualização: Jul 03, 2026

A aceleração de transferência agiliza o acesso ao OSS para usuários em todo o mundo. Esse recurso é ideal para transferências de dados de longa distância e para upload ou download de arquivos na escala de GB ou TB.

Precauções

  • O código de exemplo deste tópico usa o ID da região China (Hangzhou) (cn-hangzhou) e um endpoint público. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões do OSS e os respectivos endpoints, consulte Regiões e endpoints do OSS.

Definições de métodos

Ativar a aceleração de transferência

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

Consultar o status da aceleração de transferência

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

Parâmetros da solicitação

Parâmetro

Tipo

Descrição

request

PutBucketTransferAccelerationRequest

Parâmetros da solicitação. Para mais informações, consulte PutBucketTransferAccelerationRequest.

GetBucketTransferAccelerationRequest

Parâmetros da solicitação. Para mais informações, consulte GetBucketTransferAccelerationRequest.

Valores de retorno

Tipo

Descrição

PutBucketTransferAccelerationResult

Valor de retorno. Para mais informações, consulte PutBucketTransferAccelerationResult.

GetBucketTransferAccelerationResult

Valor de retorno. Para mais informações, consulte GetBucketTransferAccelerationResult.

Para mais informações sobre como ativar a aceleração de transferência, consulte put_bucket_transfer_acceleration.

Para mais informações sobre como consultar o status da aceleração de transferência, consulte get_bucket_transfer_acceleration.

Código de exemplo

Ativar a aceleração de transferência

O exemplo a seguir mostra como ativar a aceleração de transferência para um 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.

Consultar o status da aceleração de transferência

O exemplo a seguir mostra como consultar o status da aceleração de transferência de um 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.

Referências