Todos os produtos
Search
Central de documentação

Object Storage Service:Renomear objetos com o OSS SDK for Python 1.0

Última atualização: Jul 03, 2026

O OSS não permite renomear objetos diretamente. 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 source.

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 da Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.

  • Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para saber como configurar credenciais de acesso, consulte Configurar credenciais de acesso (Python SDK V1).

  • Este exemplo demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Inicialização.

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)
Nota

Diretórios também não podem ser renomeados diretamente. Aplique a mesma abordagem de cópia seguida de exclusão individualmente para cada subdiretório e objeto.

Referências

Para mais detalhes sobre as operações de API usadas para renomear um objeto, consulte CopyObject e DeleteObject.