Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Dosya yeniden adlandırma (Python SDK V2)

Son Güncelleme:Oct 01, 2026

OSS, nesneleri doğrudan yeniden adlandırmayı desteklemez. Aynı paket içindeki bir nesneyi yeniden adlandırmak için CopyObject işlemini kullanarak kaynak nesneyi yeni adla hedef nesneye kopyalayın ve ardından kaynak nesneyi silin.

Notlar

  • Bu konudaki örnek kod, Çin (Hangzhou) bölgesinin genel uç noktasını (cn-hangzhou) kullanır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişiyorsanız bunun yerine dahili uç noktayı kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. OSS regions and endpoints.

Örnek kod

Basit kopyalama (CopyObject) yöntemini kullanarak bir dosyayı yeniden adlandırma

Aşağıdaki örnek kod, bir dosyayı yeni bir anahtara kopyalayarak ve orijinalini silerek yeniden adlandırmak için CopyObject yöntemini kullanır.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="copy object sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This is a required parameter.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the destination bucket. This is a required parameter.
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the endpoint that other services can use to access OSS. This is an optional parameter.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the destination object. This is a required parameter.
parser.add_argument('--key', help='The name of the destination object.', required=True)
# Add the --source_key command-line argument, which specifies the name of the source object. This is a required parameter.
parser.add_argument('--source_key', help='The name of the source object.', required=True)
# Add the --source_bucket command-line argument, which specifies the name of the source bucket. This is a required parameter.
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Load credentials from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK and set the credentials provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region in the configuration.
    cfg.region = args.region

    # If an endpoint is provided, set the endpoint in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client with the specified configurations.
    client = oss.Client(cfg)

    # Send a request to copy the object.
    result = client.copy_object(oss.CopyObjectRequest(
        bucket=args.bucket,  # Specify the name of the destination bucket.
        key=args.key,  # Specify the key of the destination object.
        source_key=args.source_key,  # Specify the key of the source object.
        source_bucket=args.source_bucket,  # Specify the name of the source bucket.
    ))
    
    # Delete the original object.
    client.delete_object(oss.DeleteObjectRequest(
        bucket=args.source_bucket,
        key=args.source_key
    ))

    # Print the result of the copy operation.
    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},'
    )

# Call the main function when the script is run directly.
if __name__ == "__main__":
    main()  # Script entry point. The main function is called when the file is run directly.

Kopyalama yöneticisini (Copier) kullanarak bir dosyayı yeniden adlandırma

Aşağıdaki örnek kod, bir dosyayı yeniden adlandırmak için kopyalama yöneticisinin Copier.Copy yöntemini kullanır.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="copier sample")

# Add the --region command-line argument (required), which specifies the region where the bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument (required), which specifies the name of the destination bucket.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)

# Add the --endpoint command-line argument (optional), which specifies the endpoint to access OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

# Add the --key command-line argument (required), which specifies the name of the destination object.
parser.add_argument('--key', help='The name of the object.', required=True)

# Add the --source_key command-line argument (required), which specifies the name of the source object.
parser.add_argument('--source_key', help='The name of the source address for object.', required=True)

# Add the --source_bucket command-line argument (required), which specifies the name of the source bucket.
parser.add_argument('--source_bucket', help='The name of the source address for bucket.', required=True)

def main():
    # Parse the command-line arguments.
    args = parser.parse_args()

    # Load credentials from environment variables.
    # Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider  # Set the credentials provider.
    cfg.region = args.region  # Set the region where the bucket is located.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint  # If an endpoint is provided, set a custom endpoint.

    # Create an OSS client instance.
    client = oss.Client(cfg)

    # Create a Copier instance and copy the object.
    copier = client.copier()

    # Copy the object.
    result = copier.copy(
        oss.CopyObjectRequest(
            bucket=args.bucket,          # The name of the destination bucket.
            key=args.key,                # The name of the destination object.
            source_bucket=args.source_bucket,  # The name of the source bucket.
            source_key=args.source_key   # The name of the source object.
        )
    )

    # Print the copy result.
    # Use vars(result) to convert the result object to a dictionary and print it.
    print(vars(result))

if __name__ == "__main__":
    main()

İlgili bilgiler

  • Basit kopyalama yöntemini kullanarak bir dosyayı yeniden adlandırmaya ilişkin eksiksiz örnek kod için bkz. copy_object.py.

  • Kopyalama yöneticisini kullanarak bir dosyayı yeniden adlandırmaya ilişkin eksiksiz örnek kod için bkz. copier.py.