You need to enable JavaScript to run this app.
导航

拷贝对象(iOS SDK)

最近更新时间2024.02.04 18:31:00

首次发布时间2022.11.21 19:11:16

本文介绍拷贝对象的示例代码。拷贝不支持跨地域的拷贝,并且要求对原对象拥有读权限。

普通拷贝

如下代码展示如何在同一个桶内拷贝对象。

// 从STS服务获取的临时访问密钥和安全令牌(AccessKey、SecretKey、SecurityToken)
TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey" securityToken:@"securityToken"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSCopyObjectInput *input = [[TOSCopyObjectInput alloc] init];
input.tosBucket = @"bucket-name";
input.tosSrcBucket = @"src-bucket-name";
input.tosKey = @"object-name";
input.tosSrcKey = @"src-object-name";
input.tosMeta = @{@"new-meta-key" : @"new-meta-value"};
input.tosMetadataDirective = TOSMetadataDirectiveReplace;

TOSTask *task = [client copyObject:input];
[task continueWithBlock:^id(TOSTask *task) {
    if (!task.error) {
        NSLog(@"Copy object success.");
    } else {
        NSLog(@"Copy object failed, error: %@" ,task.error);
    }
    return nil;
}];

分片拷贝

拷贝大对象时,您可以通过 UploadPartCopy 来进行分片拷贝,步骤说明如下:

  1. 通过 CreateMultipartUpload 初始化分片拷贝任务。
  2. 通过 UploadPartCopy 进行分片拷贝。
  3. 通过 CompleteMultipartUpload 合并分片。
TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

// 1. CreateMultipartUpload
TOSCreateMultipartUploadInput *create = [TOSCreateMultipartUploadInput new];
create.tosBucket = @"bucket-name";
create.tosKey = @"object-name";
task = [client createMultipartUpload:create];
[task waitUntilFinished];
TOSCreateMultipartUploadOutput *createOutput = task.result;

// 2. UploadPartCopy
TOSUploadPartCopyInput *part1 = [TOSUploadPartCopyInput new];
part1.tosBucket = @"bucket-name";
part1.tosKey = @"object-name";
part1.tosSrcBucket = @"src-bucket-name";
part1.tosSrcKey = @"src-object-name";
part1.tosCopySourceRangeStart = @0;
part1.tosPartNumber = 1;
task = [client uploadPartCopy:part1];
[task waitUntilFinished];
TOSUploadPartCopyOutput *uploadOutput = task.result;
uploadOutput.tosETag = [uploadOutput.tosETag stringByReplacingOccurrencesOfString:@"\"" withString:@""];

// 3. CompleteMultipartUpload
TOSCompleteMultipartUploadInput *complete = [TOSCompleteMultipartUploadInput new];
complete.tosBucket = createOutput.bucket;
complete.tosKey = createOutput.key;
complete.tosUploadId = createOutput.uploadId;

NSMutableArray *array = [NSMutableArray array];
TOSUploadedPart *uploadedPart1 = [TOSUploadedPart new];
uploadedPart1.tosPartNumber = uploadOutput.tosPartNumber;
uploadedPart1.tosETag = uploadOutput.tosETag;
[array addObject:uploadedPart1];

complete.parts = array;
task = [client completeMultipartUpload:complete];
[task waitUntilFinished];
TOSCompleteMultipartUploadOutput *comleteOutput = task.result;