OSS no permite renombrar objetos directamente. Para cambiar el nombre de un objeto, ejecute una operación de copia y eliminación: invoque CopyObject para crear un nuevo objeto con el nombre deseado y luego invoque DeleteObject para eliminar el objeto de origen.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Código de ejemplo
El siguiente ejemplo renombra srcobject.txt a destobject.txt en el 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)
Los directorios tampoco se pueden renombrar directamente. Aplique el mismo método de copia y eliminación a cada subdirectorio y objeto de forma individual.
Referencias
Para obtener más información sobre las operaciones de API para renombrar un objeto, consulte CopyObject y DeleteObject.