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.
This two-step operation is not atomic. If another process modifies or deletes the source object between the copy and delete steps, the results may be inconsistent. Design your application to account for this if concurrent access is possible.
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 code renames srcobject.txt to destobject.txt in the examplebucket bucket by copying the source object to the destination and then deleting the source.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the OSS_ACCESS_KEY_ID and
# OSS_ACCESS_KEY_SECRET environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint and region of the bucket.
# Example: China (Hangzhou) region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou" # Required for V4 signatures.
# Specify the bucket name.
bucket_name = 'examplebucket'
bucket = oss2.Bucket(auth, endpoint, bucket_name, region=region)
# Specify the source and destination object names.
# Do not include the bucket name in the object paths.
src_object_name = 'srcobject.txt'
dest_object_name = 'destobject.txt'
if src_object_name == dest_object_name:
print('The source and destination names are the same. No rename is needed.')
else:
try:
# Step 1: Copy the source object to the destination.
result = bucket.copy_object(bucket_name, src_object_name, dest_object_name)
print('Copy status:', result.status) # 200 indicates success.
# Step 2: Delete the source object.
result_del = bucket.delete_object(src_object_name)
print('Delete status:', result_del.status) # 204 indicates success.
except oss2.exceptions.NoSuchKey:
print('Error: The source object does not exist.')
except oss2.exceptions.OssError as e:
print(f'OSS error: {e.status}, Code: {e.code}, Message: {e.message}')
If the copy step succeeds but the delete step fails, both the source and destination objects will exist in the bucket. In that case, retry deleting the source object or remove it manually.
Rename folders
OSS does not support renaming folders directly. To rename a folder, apply the object renaming logic to each object and subdirectory in the folder.