All Products
Search
Document Center

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

Last Updated:Feb 27, 2026

OSS does not support renaming an object directly. To rename an object within the same bucket, copy the source object to a destination object with the new name, then delete the source object.

Two copy methods are available:

  • CopyObject (simple copy): Copies an object in a single request. Use this method for small objects when you need straightforward control over the operation.

  • Copier (copy manager): Automatically handles objects of any size. For objects larger than a configurable threshold, the Copier splits the operation into multipart copies. Use this method when the object size may vary.

Prerequisites

Before you begin, make sure that you have:

  • The alibabacloud_oss_v2 SDK for Python installed

  • An AccessKey ID and AccessKey secret configured as environment variables

The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) public endpoint. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For more information about regions and endpoints, see Regions and endpoints.

Sample code

Rename an object by using CopyObject

The following sample code copies an object to a new key with CopyObject, then deletes the original object to complete the rename.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="rename object sample")

parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
parser.add_argument('--key', help='The name of the destination object.', required=True)
parser.add_argument('--source_key', help='The name of the source object.', required=True)
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)

def main():
    args = parser.parse_args()

    # Load AccessKey ID and AccessKey secret from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Initialize the SDK configuration.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    # Step 1: Copy the object to the new key.
    result = client.copy_object(oss.CopyObjectRequest(
        bucket=args.bucket,
        key=args.key,
        source_key=args.source_key,
        source_bucket=args.source_bucket,
    ))

    # Step 2: Delete the original object to complete the rename.
    client.delete_object(oss.DeleteObjectRequest(
        bucket=args.source_bucket,
        key=args.source_key
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' hash crc64: {result.hash_crc64},'
          f' source version id: {result.source_version_id},'
          f' server side encryption: {result.server_side_encryption},'
          f' server side data encryption: {result.server_side_data_encryption},'
          f' last modified: {result.last_modified},'
          f' etag: {result.etag},'
    )

if __name__ == "__main__":
    main()

Rename an object by using the Copier

The following sample code copies an object to a new key with the Copier, then deletes the original object to complete the rename. The Copier automatically switches between simple copy and multipart copy based on the object size.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="rename object sample")

parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
parser.add_argument('--key', help='The name of the destination object.', required=True)
parser.add_argument('--source_key', help='The name of the source object.', required=True)
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)

def main():
    args = parser.parse_args()

    # Load AccessKey ID and AccessKey secret from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Initialize the SDK configuration.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    # Step 1: Copy the object to the new key.
    copier = client.copier()
    result = copier.copy(
        oss.CopyObjectRequest(
            bucket=args.bucket,
            key=args.key,
            source_bucket=args.source_bucket,
            source_key=args.source_key
        )
    )

    # Step 2: Delete the original object to complete the rename.
    client.delete_object(oss.DeleteObjectRequest(
        bucket=args.source_bucket,
        key=args.source_key
    ))

    print(vars(result))

if __name__ == "__main__":
    main()

References

  • For the complete sample code that uses the simple copy method, see copy_object.py.

  • For the complete sample code that uses the copy manager, see copier.py.