O OSS não oferece suporte à renomeação direta de objetos. Para renomear um objeto, execute uma operação de cópia seguida de exclusão: chame CopyObject para criar um novo objeto com o nome desejado e, em seguida, chame DeleteObject para remover o objeto de origem.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Caso precise acessar o OSS a partir de outros serviços do Alibaba Cloud na mesma região do OSS, utilize um endpoint interno. Para mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, as credenciais de acesso são obtidas a partir de variáveis de ambiente. Para mais informações sobre como configurar as credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como o uso de um domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization.
Código de exemplo
O exemplo a seguir renomeia srcobject.txt para destobject.txt no bucket examplebucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket_name = 'examplebucket'
bucket = oss2.Bucket(auth, endpoint, bucket_name, region=region)
# Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcobject.txt.
src_object_name = 'srcobject.txt'
# Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destobject.txt.
dest_object_name = 'destobject.txt'
# Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket.
result = bucket.copy_object(bucket_name, src_object_name, dest_object_name)
# View the response. If HTTP status code 200 is returned, the operation is successful.
print('result.status:', result.status)
# Delete the srcobject.txt object.
result_del = bucket.delete_object(src_object_name)
# View the response. If HTTP status code 204 is returned, the operation is successful.
print('result.status:', result_del.status)
Também não é possível renomear diretórios diretamente. Aplique a mesma abordagem de cópia seguida de exclusão para cada subdiretório e objeto individualmente.
Referências
Para mais informações sobre as operações de API para renomear um objeto, consulte CopyObject e DeleteObject.