Tous les produits
Search
Centre de documentation

Object Storage Service:Renommer des objets à l'aide du SDK OSS pour Python 1.0

Dernière mise à jour :Sep 22, 2026

OSS ne permet pas de renommer directement les objets. Pour renommer un objet, copiez-le puis supprimez l'original : appelez CopyObject pour créer un nouvel objet avec le nom souhaité, puis appelez DeleteObject pour supprimer l'objet source.

Notes

  • Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

  • Les identifiants d'accès sont récupérés depuis les variables d'environnement. Pour savoir comment les configurer, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.

  • Cette rubrique montre comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation.

Exemple de code

L'exemple suivant renomme srcobject.txt en destobject.txt dans le 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)
Remarque

Il est également impossible de renommer directement les répertoires. Appliquez la même procédure de copie-suppression à chaque sous-répertoire et objet individuellement.

Références

Pour plus d'informations sur les opérations API liées au renommage d'un objet, consultez CopyObject et DeleteObject.