Tous les produits
Search
Centre de documentation

Object Storage Service:Copier un fichier (SDK iOS)

Dernière mise à jour :Aug 18, 2026

Copiez des objets au sein d’un même compartiment ou entre des compartiments situés dans la même région.

Notes

  • Avant d’utiliser les exemples présentés dans cette rubrique, créez une instance OSSClient en utilisant un nom de domaine personnalisé, Security Token Service (STS) ou toute autre méthode appropriée. Pour plus d’informations, consultez Initialisation (SDK iOS).

  • Lorsque vous copiez un fichier, vous devez disposer des autorisations de lecture sur le fichier source ainsi que des autorisations de lecture et d’écriture sur le compartiment de destination.

  • Les opérations de copie interrégionales ne sont pas prises en charge. Par exemple, il est impossible de copier un fichier depuis un compartiment situé dans la région Chine (Hangzhou) vers un compartiment situé dans la région Chine (Qingdao).

  • La taille du fichier à copier ne doit pas dépasser 1 Go.

Permissions

Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud n’ont aucune autorisation par défaut. Le propriétaire du compte Alibaba Cloud ou l’administrateur du compte doit accorder les autorisations d’opération via des stratégies RAM ou une Bucket Policy.

API

Action

Description

CopyObject

oss:GetObject

Copie des objets au sein d’un compartiment ou entre des compartiments situés dans la même région.

oss:PutObject

oss:GetObjectVersion

Si vous spécifiez la version de l’objet source via versionId, cette autorisation est également requise.

oss:GetObjectTagging

Si vous copiez les tags d’objet via x-oss-tagging, ces autorisations sont nécessaires.

oss:PutObjectTagging

oss:GetObjectVersionTagging

Si vous spécifiez les tags d’une version particulière de l’objet source via versionId, cette autorisation est également requise.

kms:GenerateDataKey

Lors de la copie d’un objet, si les métadonnées de l’objet de destination contiennent X-Oss-Server-Side-Encryption: KMS, ces deux autorisations sont nécessaires.

kms:Decrypt

Exemple de code

Le code suivant illustre la procédure de copie d’un fichier :

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];

Références

Pour plus d’informations sur l’opération API CopyObject, consultez CopyObject.