All Products
Search
Document Center

Object Storage Service:Rename objects using OSS SDK for Python 1.0

Last Updated:Jun 03, 2026

OSS does not support renaming objects directly. To rename an object in the same bucket, call the CopyObject operation to copy the source object to a destination object. Then, call the DeleteObject operation to delete the source object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.

Sample code

The following example renames srcobject.txt to destobject.txt in the examplebucket bucket:

# -*- 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)
Note

Directories cannot be directly renamed either. Apply the same copy-then-delete approach to each subdirectory and object individually.

References

For more information about the API operations for renaming an object, see CopyObject and DeleteObject.