Todos os produtos
Search
Central de documentação

Object Storage Service:Upload por anexação (Python SDK V2)

Última atualização: Sep 22, 2026

O upload por anexação permite anexar dados ao final de um objeto anexável existente. Este tópico descreve como fazer um upload por anexação usando o OSS SDK for Python V2.

Precauções

  • O código de exemplo neste tópico usa a região China (Hangzhou), cujo ID de região é cn-hangzhou, como exemplo. Por padrão, o SDK usa um endpoint público. Se você quiser acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre os mapeamentos entre regiões e endpoints do OSS, consulte OSS regions and endpoints.

  • Se o objeto não existir, a chamada ao método de upload por anexação cria um objeto anexável.

  • Se o objeto já existir:

    • Se o objeto for anexável e a posição de anexação especificada for igual ao comprimento atual do objeto, o conteúdo é anexado ao final do objeto.

    • Se o objeto for anexável, mas a posição de anexação especificada não for igual ao comprimento atual do objeto, uma exceção PositionNotEqualToLength é lançada.

    • Se o objeto não for anexável, como um objeto normal enviado por meio de upload simples, uma exceção ObjectNotAppendable é lançada.

Permissões

Por padrão, uma conta Alibaba Cloud tem permissões completas. Os usuários RAM e as funções RAM sob uma conta Alibaba Cloud não têm permissões por padrão. A conta Alibaba Cloud ou o administrador da conta deve conceder as permissões de operação por meio de RAM policies ou Bucket Policy.

API

Ação

Descrição

AppendObject

oss:PutObject

Use esta operação para fazer o upload de um objeto anexando-o a um objeto existente.

oss:PutObjectTagging

Ao fazer o upload de um objeto anexando-o a um objeto existente, se você especificar tags do objeto por meio de x-oss-tagging, essa permissão é necessária.

Definições de métodos

Para cenários de upload por anexação, o Python SDK V2 adiciona o método AppendFile para simular operações de leitura e gravação de arquivos em objetos de um bucket. A tabela a seguir descreve os métodos AppendFile e AppendObject.

Método

Descrição

AppendFile

Fornece os mesmos recursos que o método AppendObject.

Otimiza a tolerância a falhas em retransmissões após uma falha.

AppendObject

Executa um upload por anexação. O objeto final pode ter até 5 GiB de tamanho.

Suporta validação de dados CRC-64 (ativada por padrão).

Suporta barras de progresso.

AppendFile: A API de upload por anexação da Premium Edition

Chame o método AppendFile para fazer o upload de dados no modo de anexação. Se o objeto não existir, um objeto anexável é criado. Se o objeto existir, mas não for anexável, um erro é retornado.

O código a seguir mostra a definição do método AppendFile.

append_file(bucket: str, key: str, request_payer: str | None = None, create_parameter: AppendObjectRequest | None = None, **kwargs) → AppendOnlyFile

Parâmetros de requisição

Parâmetro

Tipo

Descrição

bucket

str

O nome do bucket.

key

str

O nome do objeto.

RequestPayer

str

Se o modo de pagamento pelo solicitante estiver ativado, defina este parâmetro como 'requester'.

CreateParameter

AppendObjectRequest

Os metadados do objeto configurados para o primeiro upload, incluindo ContentType, Metadata, permissões e classe de armazenamento. Para obter mais informações, consulte AppendObjectRequest.

Valores de retorno

Tipo

Descrição

AppendOnlyFile

A instância do arquivo anexável. Para obter mais informações, consulte AppendOnlyFile.

A tabela a seguir descreve os métodos incluídos na classe AppendOnlyFile.

Método

Descrição

Close()

Fecha o identificador do arquivo e libera os recursos.

write(b)

Grava dados em bytes no arquivo e retorna o número de bytes gravados.

write_from(b: str

bytes

Iterable[bytes]

IO[str]

IO[bytes])

Grava quaisquer dados no arquivo e retorna o número de bytes gravados.

Para a definição completa do método AppendFile, consulte append_file.

AppendObject: A API de upload por anexação da Basic Edition

append_object(request: AppendObjectRequest, **kwargs) → AppendObjectResult

Parâmetros de requisição

Parâmetro

Tipo

Descrição

request

AppendObjectRequest

Os parâmetros da requisição. Para obter mais informações, consulte AppendObjectRequest.

Valores de retorno

Tipo

Descrição

AppendObjectResult

O valor de retorno. Para obter mais informações, consulte AppendObjectResult.

Para a definição completa do método AppendObject, consulte append_object.

Exemplos

(Recomendado) Use o AppendFile para fazer um upload por anexação

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: This example shows how to append data to an OSS object.
parser = argparse.ArgumentParser(description="append file sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket to operate on. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the key of the object (file) in OSS. This argument is required.
parser.add_argument('--key', help='The name of the object.', required=True)

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

    # Load the authentication information required to access OSS from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configurations of the SDK and set the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    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 with the preceding configurations to prepare for interaction with OSS.
    client = oss.Client(cfg)

    # Define the data to be appended.
    data1 = b'hello'
    data2 = b' world. '

    # Append data for the first time.
    with client.append_file(bucket=args.bucket, key=args.key) as f:
        append_f = f
        f.write(data1)
    # Print the file status after the first append operation.
    print(f'closed: {append_f.closed},'
          f' name: {append_f.name}'
    )

    # Append data for the second time.
    with client.append_file(bucket=args.bucket, key=args.key) as f:
        append_f = f
        f.write(data2)
    # Print the file status after the second append operation.
    print(f'closed: {append_f.closed},'
          f' name: {append_f.name}'
    )

    # Obtain the content of the object after appending data.
    result = client.get_object(oss.GetObjectRequest(
        bucket=args.bucket,
        key=args.key,
    ))
    # Print the result of obtaining the object.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' content: {result.body.content.decode("utf-8")}'
    )

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

Use o AppendObject para fazer um upload por anexação

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="append object sample")

# Add command-line arguments.
# --region: Specifies the region where the OSS bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# --bucket: Specifies the name of the bucket to operate on.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# --endpoint: An optional parameter that specifies the domain name used to access the OSS service.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# --key: Specifies the key of the object (file) in OSS.
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Load the authentication information required for OSS from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configurations provided by the SDK.
    cfg = oss.config.load_default()

    # Set the credential provider to the previously created object.
    cfg.credentials_provider = credentials_provider

    # Set the region for the OSS client based on user input.
    cfg.region = args.region

    # If the user provides a custom endpoint, update the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client instance using the preceding configurations.
    client = oss.Client(cfg)

    # Define the data to be appended.
    data1 = b'hello'
    data2 = b' world'

    # Append data for the first time.
    result = client.append_object(oss.AppendObjectRequest(
        bucket=args.bucket,  # Specify the destination bucket.
        key=args.key,  # Specify the key of the object.
        position=0,  # The starting position for appending, which is initially 0.
        body=data1,  # The data to be appended.
    ))

    # Print the result of the first append operation.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' hash crc64: {result.hash_crc64},'
          f' next position: {result.next_position},' 
    )

    # Append data for the second time.
    result = client.append_object(oss.AppendObjectRequest(
        bucket=args.bucket,  # Specify the destination bucket.
        key=args.key,  # Specify the key of the object.
        position=result.next_position,  # Start from the next position of the previous append operation.
        body=data2,  # The data to be appended.
    ))

    # Print the result of the second append operation.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' hash crc64: {result.hash_crc64},'
          f' next position: {result.next_position},'
    )

# When this script is run directly, call the main function.
if __name__ == "__main__":
    main()

Cenários

Exiba uma barra de progresso para um upload por anexação

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="append object sample")

# Add command-line arguments.
# --region: Specifies the region where the OSS bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# --bucket: Specifies the name of the bucket to operate on.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# --endpoint: An optional parameter that specifies the domain name used to access the OSS service.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# --key: Specifies the key of the object (file) in OSS.
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Load the authentication information required for OSS from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configurations provided by the SDK.
    cfg = oss.config.load_default()

    # Set the credential provider to the previously created object.
    cfg.credentials_provider = credentials_provider

    # Set the region for the OSS client based on user input.
    cfg.region = args.region

    # If the user provides a custom endpoint, update the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client instance using the preceding configurations.
    client = oss.Client(cfg)

    # Define a dictionary variable named progress_state to save the upload progress status. The initial value is 0.
    progress_state = {'saved': 0}
    def _progress_fn(n, written, total):
        # Use a dictionary to store the cumulative number of written bytes to avoid using global variables.
        progress_state['saved'] += n

        # Calculate the current upload percentage. The value is obtained by dividing the number of written bytes by the total number of bytes and rounding down the result.
        rate = int(100 * (float(written) / float(total)))

        # Print the current upload progress. \r indicates returning to the beginning of the line to implement real-time refresh in the command line.
        # end='' indicates no line break, so that the next printout overwrites the current line.
        print(f'\rUpload progress: {rate}% ', end='')

    # Define the data to be appended.
    data1 = b'hello'
    data2 = b' world'

    # Append data for the first time.
    result = client.append_object(oss.AppendObjectRequest(
        bucket=args.bucket,  # Specify the destination bucket.
        key=args.key,  # Specify the key of the object.
        position=0,  # The starting position for appending, which is initially 0.
        body=data1,  # The data to be appended.
        progress_fn=_progress_fn,  # Set the progress callback function.
    ))

    # Print the result of the first append operation.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' hash crc64: {result.hash_crc64},'
          f' next position: {result.next_position},'
    )

    # Append data for the second time.
    result = client.append_object(oss.AppendObjectRequest(
        bucket=args.bucket,  # Specify the destination bucket.
        key=args.key,  # Specify the key of the object.
        position=result.next_position,  # Start from the next position of the previous append operation.
        body=data2,  # The data to be appended.
        progress_fn=_progress_fn,  # Set the progress callback function.
    ))

    # Print the result of the second append operation.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' hash crc64: {result.hash_crc64},'
          f' next position: {result.next_position},'
    )

# When this script is run directly, call the main function.
if __name__ == "__main__":
    main()

Referências