Copie objetos dentro de un mismo bucket o entre buckets de la misma región.
Notas
Antes de usar los ejemplos de este tema, cree una instancia de OSSClient con un nombre de dominio personalizado, Security Token Service (STS) u otros métodos. Para obtener más información, consulte Initialization (iOS SDK).
Para copiar un archivo, necesita permisos de lectura sobre el archivo de origen y permisos de lectura y escritura en el bucket de destino.
No se admiten las operaciones de copia entre regiones. Por ejemplo, no es posible copiar un archivo de un bucket en la región China (Hangzhou) a un bucket en la región China (Qingdao).
El tamaño del archivo que se va a copiar no puede superar 1 GB.
Permisos
De forma predeterminada, una cuenta de Alibaba Cloud tiene todos los permisos. Los usuarios o roles de RAM asociados a una cuenta de Alibaba Cloud no cuentan con ningún permiso de manera predeterminada. La cuenta de Alibaba Cloud o su administrador deben otorgar los permisos de operación mediante RAM policies o Bucket Policy.
|
API |
Action |
Descripción |
|
CopyObject |
|
Copia objetos dentro de un mismo bucket o entre buckets de la misma región. |
|
|
||
|
|
Este permiso también es obligatorio si especifica la versión del objeto de origen mediante versionId. |
|
|
|
Estos permisos son obligatorios si copia las etiquetas del objeto mediante x-oss-tagging. |
|
|
|
||
|
|
Este permiso también es obligatorio si especifica las etiquetas de una versión específica del objeto de origen mediante versionId. |
|
|
|
Estos dos permisos son obligatorios al copiar un objeto si los metadatos del objeto de destino contienen X-Oss-Server-Side-Encryption: KMS. |
|
|
|
Código de ejemplo
A continuación se muestra un ejemplo de cómo copiar un archivo:
OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
// Specify the name of the source bucket.
copy.sourceBucketName = @"sourcebucket";
// Specify the full path of the object in the source bucket.
copy.sourceObjectKey = @"dir1/srcobject.txt";
// Specify the name of the destination bucket.
copy.bucketName = @"destbucket";
// Specify the full path of the object in the destination bucket.
copy.objectKey = @"dir2/destobject.txt";
NSMutableDictionary *objectMeta = [NSMutableDictionary dictionary];
// Set the access control list (ACL). In this example, the value is set to public-read.
[objectMeta setValue:@"public-read" forKey:@"x-oss-object-acl"];
// Set the storage class. In this example, the value is set to Standard.
[objectMeta setValue:@"Standard" forKey:@"x-oss-storage-class"];
// Specify whether to overwrite an object that has the same name. If you do not specify x-oss-forbid-overwrite, the object is overwritten by default.
// If you set x-oss-forbid-overwrite to false, the object can be overwritten. If you set x-oss-forbid-overwrite to true, the object cannot be overwritten. If an object with the same name exists, an error is reported.
[objectMeta setValue:@"true" forKey:@"x-oss-forbid-overwrite"];
// The copy operation is performed only if the ETag of the source object matches the ETag you provide.
[objectMeta setValue:@"5B3C1A2E053D763E1B002CC607C5****" forKey:@"x-oss-copy-source-if-match"];
// The copy operation is performed only if the ETag of the source object does not match the ETag you provide.
[objectMeta setValue:@"5B3C1A2E053D763E1B002CC607C5****" forKey:@"x-oss-copy-source-if-none-match"];
// The copy operation is performed only if the object was last modified on or before 2021-12-09T07:01:56.000Z.
[objectMeta setValue:@"2021-12-09T07:01:56.000Z" forKey:@"x-oss-copy-source-if-unmodified-since"];
// The copy operation is performed only if the object was last modified after 2021-12-15T07:01:56.000Z.
[objectMeta setValue:@"2021-12-15T07:01:56.000Z" forKey:@"x-oss-copy-source-if-modified-since"];
// Copy the metadata from the source object to the destination object.
[objectMeta setValue:@"COPY" forKey:@"x-oss-metadata-directive"];
// Copy the tags from the source object to the destination object.
[objectMeta setValue:@"Copy" forKey:@"x-oss-tagging-directive"];
// Specify the server-side encryption algorithm that OSS uses to create the destination object.
[objectMeta setValue:@"KMS" forKey:@"x-oss-server-side-encryption"];
// The customer master key (CMK) that is managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS.
[objectMeta setValue:@"9468da86-3509-4f8d-a61e-6eab1eac****" forKey:@"x-oss-server-side-encryption-key-id"];
copy.objectMeta = objectMeta;
OSSTask * task = [client copyObject:copy];
[task continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"Copy object success!");
} else {
NSLog(@"Copy object failed. Error: %@" , task.error);
}
return nil;
}];
// Block the current thread to wait for the task to complete.
// [task waitUntilFinished];
Referencias
Para obtener más información sobre la operación de API CopyObject, consulte CopyObject.