All Products
Search
Document Center

Object Storage Service:Copy a file (iOS SDK)

Last Updated:Dec 05, 2025

This topic describes how to copy an object within a bucket or across buckets in the same region.

Notes

  • Before you use the examples in this topic, create an OSSClient instance using a custom domain name, Security Token Service (STS), or other methods. For more information, see Initialization (iOS SDK).

  • When you copy a file, you must have read permissions on the source file and read/write permissions for the destination bucket.

  • Cross-region copy operations are not supported. For example, you cannot copy a file from a bucket in the China (Hangzhou) region to a bucket in the China (Qingdao) region.

  • The size of the file to be copied cannot exceed 1 GB.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.

API

Action

Definition

CopyObject

oss:GetObject

Copies objects within a bucket or between buckets in the same region.

oss:PutObject

oss:GetObjectVersion

If you specify the source object version through versionId, this permission is also required.

oss:GetObjectTagging

If you copy object tags through x-oss-tagging, these permissions are required.

oss:PutObjectTagging

oss:GetObjectVersionTagging

If you specify the tags of a specific version of the source object through versionId, this permission is also required.

kms:GenerateDataKey

When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required.

kms:Decrypt

Sample code

The following code provides an example of how to copy a file:

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

References

For more information about the CopyObject API operation, see CopyObject.