OSS, nesnelerin doğrudan yeniden adlandırılmasını desteklemez. Bir nesneyi yeniden adlandırmak için kopyala-sonra-sil yöntemini uygulayın: İstenen ada sahip yeni bir nesne oluşturmak için CopyObject işlevini çağırın, ardından kaynak nesneyi kaldırmak için DeleteObject işlevini çağırın.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Python için OSS SDK 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası ile bir OSSClient örneği oluşturmayı göstermektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma.
Örnek kod
Aşağıdaki örnek, examplebucket paketindeki srcobject.txt dosyasını destobject.txt olarak yeniden adlandırır:
# -*- 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)
Dizinler de doğrudan yeniden adlandırılamaz. Her alt dizin ve nesne için aynı kopyala-sonra-sil yaklaşımını ayrı ayrı uygulayın.
İlgili Başlıklar
Bir nesneyi yeniden adlandırmaya yönelik API işlemleri hakkında daha fazla bilgi için bkz. CopyObject ve DeleteObject.