Nesneleri bir paket içinde veya aynı bölgedeki paketler arasında kopyalayın.
Dikkat edilmesi gerekenler
Bir dosyayı kopyalamak için kaynak dosya üzerinde okuma iznine ve hedef paket üzerinde okuma ve yazma iznine sahip olmanız gerekir.
Bölgeler arası kopyalama desteklenmez. Örneğin, China (Hangzhou) bölgesindeki bir paketten China (Qingdao) bölgesindeki bir pakete dosya kopyalayamazsınız.
Kopyalanacak dosyanın boyutu 1 GB'ı aşamaz.
Bu konudaki örnek kodu çalıştırmadan önce özel etki alanı adı veya Security Token Service (STS) gibi yöntemleri kullanarak bir OSSClient örneği oluşturmanız gerekir. Daha fazla bilgi için bkz. Başlatma (Android SDK).
İzinler
Varsayılan olarak bir Alibaba Cloud hesabı tam izinlere sahiptir. Bir Alibaba Cloud hesabı altındaki RAM kullanıcıları veya RAM rolleri varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı veya hesap yöneticisi işlem izinlerini RAM ilkeleri veya Paket İlkesi aracılığıyla vermelidir.
|
API |
Eylem |
Açıklama |
|
CopyObject |
|
Nesneleri bir paket içinde veya aynı bölgedeki paketler arasında kopyalar. |
|
|
||
|
|
Kaynak nesne sürümünü versionId ile belirtirseniz bu izin de gereklidir. |
|
|
|
Nesne etiketlerini x-oss-tagging aracılığıyla kopyalarsanız bu izinler gereklidir. |
|
|
|
||
|
|
Kaynak nesnenin belirli bir sürümünün etiketlerini versionId ile belirtirseniz bu izin de gereklidir. |
|
|
|
Bir nesneyi kopyalarken hedef nesne meta verileri X-Oss-Server-Side-Encryption: KMS içeriyorsa bu iki izin gereklidir. |
|
|
|
Örnek kod
Aşağıdaki kod bir dosyanın nasıl kopyalanacağını göstermektedir:
// Specify the name of the source bucket.
String srcBucketName = "src-bucket";
// Specify the full path of the object in the source bucket.
String srcObjectKey = "dir1/source-object.txt";
// Specify the name of the destination bucket, which must be in the same region as the source bucket.
String destBucketName = "dest-bucket";
// Specify the full path of the object in the destination bucket.
String destObjectKey = "dir2/destination-object.txt";
// Create a copy request.
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcObjectKey, destBucketName, destObjectKey);
// ObjectMetadata objectMetadata = new ObjectMetadata();
// Set the access control list (ACL) of the object. In this example, the ACL is set to private.
// objectMetadata.setHeader("x-oss-object-acl", "private");
// Set the storage class of the object. In this example, the storage class is set to Standard.
// objectMetadata.setHeader("x-oss-storage-class", "Standard");
// Specify whether to overwrite an existing object that has the same name in the destination bucket. In this example, the value is set to true, which indicates that the existing object cannot be overwritten.
// objectMetadata.setHeader("x-oss-forbid-overwrite", "true");
// The copy operation is performed only if the ETag of the source object matches the specified ETag.
// objectMetadata.setHeader("x-oss-copy-source-if-match", "5B3C1A2E053D763E1B002CC607C5****");
// Specify the source address for the copy operation.
// objectMetadata.setHeader("x-oss-copy-source", "/example-bucket/recode-test.txt");
// The copy operation is performed only if the ETag of the source object does not match the specified ETag.
// objectMetadata.setHeader("x-oss-copy-source-if-none-match", "5B3C1A2E053D763E1B002CC607C5****");
// The copy operation is performed only if the actual modification time of the object is earlier than or the same as the specified time.
// objectMetadata.setHeader("x-oss-copy-source-if-unmodified-since", "2021-12-09T07:01:56.000Z");
// The copy operation is performed only if the source object was modified after the specified time.
// objectMetadata.setHeader("x-oss-copy-source-if-modified-since", "2021-12-09T07:01:56.000Z");
// Specify how to configure the metadata of the destination object. In this example, the value is set to COPY, which indicates that the metadata of the source object is copied to the destination object.
// objectMetadata.setHeader("x-oss-metadata-directive", "COPY");
// Specify the server-side encryption algorithm that OSS uses to create the destination object.
// objectMetadata.setHeader("x-oss-server-side-encryption", "SSE-KMS");
// The customer master key (CMK) managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS.
// objectMetadata.setHeader("x-oss-server-side-encryption-key-id", "9468da86-3509-4f8d-a61e-6eab1eac****");
// Specify the tags of the object. You can specify multiple tags at the same time.
// objectMetadata.setHeader("x-oss-tagging", "a:1");
// Specify how to configure the tags of the destination object. In this example, the value is set to COPY, which indicates that the tags of the source object are copied to the destination object.
// objectMetadata.setHeader("x-oss-tagging-directive", "COPY");
// Perform an asynchronous copy.
OSSAsyncTask copyTask = oss.asyncCopyObject(copyObjectRequest, new OSSCompletedCallback<CopyObjectRequest, CopyObjectResult>() {
@Override
public void onSuccess(CopyObjectRequest request, CopyObjectResult result) {
Log.d("copyObject", "copy success!");
}
@Override
public void onFailure(CopyObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// The request failed.
if (clientExcepion != null) {
// A client exception occurred, such as a network exception.
clientExcepion.printStackTrace();
}
if (serviceException != null) {
// A server-side exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
İlgili bilgiler
Bir dosya kopyalamaya ilişkin eksiksiz örnek kod için bkz. GitHub'daki örnek.
Bir dosya kopyalama API işlemi hakkında bilgi için bkz. CopyObject.
Bir OSSClient örneğini başlatma hakkında bilgi için bkz. Android için bir OSSClient örneği başlatma.