Copie objetos dentro de um mesmo bucket ou entre buckets na mesma região.
Observações
Antes de usar os exemplos deste tópico, crie uma instância do OSSClient usando um nome de domínio personalizado, o Security Token Service (STS) ou outros métodos. Para mais informações, consulte Initialization (iOS SDK).
Ao copiar um arquivo, você deve ter permissões de leitura no arquivo de origem e permissões de leitura e gravação no bucket de destino.
Não há suporte para operações de cópia entre regiões. Por exemplo, não é possível copiar um arquivo de um bucket na região China (Hangzhou) para um bucket na região China (Qingdao).
O tamanho do arquivo a ser copiado não pode exceder 1 GB.
Permissões
Por padrão, uma conta Alibaba Cloud tem permissões totais. Usuários RAM ou funções RAM sob uma conta Alibaba Cloud não possuem nenhuma permissão por padrão. A conta Alibaba Cloud ou o administrador da conta deve conceder as permissões de operação por meio de RAM policies ou Bucket Policy.
|
API |
Ação |
Descrição |
|
CopyObject |
|
Copia objetos dentro de um mesmo bucket ou entre buckets na mesma região. |
|
|
||
|
|
Caso especifique a versão do objeto de origem por meio do versionId, esta permissão também será necessária. |
|
|
|
Ao copiar as tags do objeto por meio do x-oss-tagging, estas permissões são obrigatórias. |
|
|
|
||
|
|
Caso especifique as tags de uma versão específica do objeto de origem por meio do versionId, esta permissão também será necessária. |
|
|
|
Ao copiar um objeto, se os metadados do objeto de destino contiverem X-Oss-Server-Side-Encryption: KMS, estas duas permissões serão exigidas. |
|
|
|
Código de exemplo
O código a seguir apresenta um exemplo de como copiar um arquivo:
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];
Referências
Para mais informações sobre a operação da API CopyObject, consulte CopyObject.