Copy Object
Updated at:2025-11-03
Simply Copy Object
Basic workflow
- Instantiate the BOSClient class.
- Execute the
BOSClient.copyObjectmethod. - Return a
BOSCopyObjectResponseinstance. TheeTagand last modification timestamp can be accessed viaeTag/lastModified, among other methods.
Example code
Swift
1BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
2request.bucket = @"<bucketname>";
3request.key = @"<objectname>";
4request.source = @"<sourceBucket>/<sourceObject";
5__block BOSCopyObjectResponse* response = nil;
6BCETask* task = [client copyObject:request];
7task.then(^(BCEOutput* output) {
8 if (output.response) {
9 response = (BOSCopyObjectResponse*)output.response;
10 NSLog(@"copy obj success!");
11 }
12 if (output.error) {
13 NSLog(@"copy obj failure");
14 }
15});
16[task waitUtilFinished];
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4 // Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
12 request.bucket = @"<bucketname>";
13 request.key = @"<objectname>";
14 request.source = @"<sourceBucket>/<sourceObject";
15 __block BOSCopyObjectResponse* response = nil;
16 BCETask* task = [client copyObject:request];
17 task.then(^(BCEOutput* output) {
18 if (output.response) {
19 response = (BOSCopyObjectResponse*)output.response;
20 NSLog(@"copy obj success!");
21 }
22 if (output.error) {
23 NSLog(@"copy obj failure");
24 }
25 });
26 [task waitUtilFinished];
27}
Note: The copyObject method returns a
BOSCopyObjectResponseobject containing the new object's ETag and modification time.
Copy object with specified conditions
Copy an object with conditions specified. This functionality is typically applied in scenarios like:
- Copy an object while resetting its metadata.
- Resetting the metadata of an existing object by setting both the source and the target to the same object.
- Copy when the eTag of the source object is the same as the specified eTag;
- Copy when the eTag of the source object is the different from the specified eTag;
- Copy when the source object has not been modified after the specified time;
- Copy when the source object has been modified after the specified time;
Details as follows:
Basic workflow
- Create an instance of the BOSCopyObjectRequest class, and pass in the parameters
<source>,<ifMatchEtag>,<ifNotMatchEtag>,<ifModifiedSince>,<ifUnmodifiedSince>,<metadataDirective>. - Return a
BOSCopyObjectResponseinstance. TheeTagand last modification timestamp can be accessed viaeTag/lastModified, among other methods.
Example code
Swift
1BOSCopyObjectRequest* request = [[BOSCopyObjectRequest alloc] init];
2request.bucket = @"<bucketname>";
3request.key = @"<objectname>";
4request.source = @"<sourceBucket>/<sourceObject";
5request.metadataDirective = @"replace";
6request.ifModifiedSince = @"Wed, 01 Mar 2006 12:00:00 GMT";
7__block BOSCopyObjectResponse* response = nil;
8BCETask* task = [client copyObject:request];
9task.then(^(BCEOutput* output) {
10 if (output.response) {
11 response = (BOSCopyObjectResponse*)output.response;
12 NSLog(@"copy obj success!");
13 }
14 if (output.error) {
15 NSLog(@"copy obj failure");
16 }
17});
18[task waitUtilFinished];
Note:
BOSCopyObjectRequestallows users to modify the ObjectMeta of the target object and also provides the setting of theMatchingETagConstraintsparameter.
