All Products
Search
Document Center

Object Storage Service:Rename a file (Python SDK V1)

Last Updated:Aug 08, 2025

Objects cannot be directly renamed. To rename an object in a bucket, you can call the CopyObject operation to copy the source object to the destination object and 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

Example code

The following code shows how to rename srcobject.txt in the examplebucket bucket to destobject.txt.

# -*- 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 in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the endpoint is located, for example, cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set bucket_name to the name of your bucket, for example, examplebucket.
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. Example: srcobject.txt.
src_object_name = 'srcobject.txt'
# Specify the full path of the destination object. Do not include the bucket name. Example: destobject.txt.
dest_object_name = 'destobject.txt'

# Copy srcobject.txt in the examplebucket bucket to destobject.txt in the same bucket.
result = bucket.copy_object(bucket_name, src_object_name, dest_object_name)

# View the status of the returned result. A return value of 200 indicates that the operation is successful.
print('result.status:', result.status)

# Delete srcobject.txt.
result_del = bucket.delete_object(src_object_name)

# View the status of the returned result. A return value of 204 indicates that the operation is successful.
print('result.status:', result_del.status)
Note

OSS does not support renaming folders directly. To rename a folder, you must apply the file renaming logic to each object and subdirectory in the folder.

References

For more information about the API operations that you can call to rename an object, see CopyObject and DeleteObject.