Copy Object
Updated at:2025-11-03
Simply Copy Object
Basic workflow
- Initialize an instance of the BOSClient class.
- Execute the BosClient.copyObject( ) method.
- Returns an instance of the CopyObjectResult class. The last modification time and eTag can be viewed through the
lastModifieandeTagmembers.
Example code
Plain Text
1let copyObjectResult: CopyObjectResult;
2try {
3 let destObjectName = objectName + ".cp"; // Name of the target object for copying
4 copyObjectResult = await bosClient.copyObject(bucketName, destObjectName, bucketName, objectName);
5 logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
6} catch (bosResponse) {
7 logger.error(`errCode: ${bosResponse.error.code}`)
8 logger.error(`requestId: ${bosResponse.error.requestId}`)
9 logger.error(`errMessage: ${bosResponse.error.message}`)
10 logger.error(`statusCode: ${bosResponse.statusCode}`)
11}
Complete example
Plain Text
1import { logger, Credential, BosClient, ClientOptions } from "bos"
2import { CopyObjectResult } from "bos/src/main/ets/bos/api/DataType"
3
4 let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //Temporary AK/SK and Token returned by STS
5let clientOptions = new ClientOptions();
6 clientOptions.endpoint = "bj.bcebos.com"; //Pass in the domain name of the region where the bucket is located
7 let bosClient = new BosClient(credential, clientOptions); // Create BosClient
8let bucketName = "test-harmony-bucket";
9 let objectName = "test-object"; // Name of the source object for copying
10
11let copyObjectResult: CopyObjectResult;
12try {
13 let destObjectName = objectName + ".cp"; // Name of the target object for copying
14 copyObjectResult = await bosClient.copyObject(bucketName, destObjectName, bucketName, objectName);
15 logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
16} catch (bosResponse) {
17 logger.error(`errCode: ${bosResponse.error.code}`)
18 logger.error(`requestId: ${bosResponse.error.requestId}`)
19 logger.error(`errMessage: ${bosResponse.error.message}`)
20 logger.error(`statusCode: ${bosResponse.statusCode}`)
21}
Copy an object via CopyObjectArgs
You can also copy object via CopyObjectArgs. This function is typically used in the following scenarios:
- Copy an object and reset its metadata.
- Reset the metadata for an existing object (set both the source object and the destination object to the same object).
Basic workflow
- Create an instance of the CopyObjectArgs class, and set parameters such as
metadataDirective/ifMatch/userMeta. For their meanings, please refer to copyObject Parameter Settings. - Call the copyObject method and pass in the instance of the CopyObjectArgs class
Example code
Plain Text
1import TreeMap from '@ohos.util.TreeMap';
2import { logger, Credential, BosClient, ClientOptions } from "bos"
3import { CopyObjectResult, CopyObjectArgs } from "bos/src/main/ets/bos/api/DataType"
4
5 let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //Temporary AK/SK and Token returned by STS
6let clientOptions = new ClientOptions();
7 clientOptions.endpoint = "bj.bcebos.com"; //Pass in the domain name of the region where the bucket is located
8 let bosClient = new BosClient(credential, clientOptions); // Create BosClient
9
10 // The destination bucket and destination object for copying
11let destBucketName = "dest-bucket";
12let destObjectName = "dest-object";
13
14 // The source bucket and source object for copying
15let srcBucketName = "src-bucket"
16let srcObjectName = "src-object";
17
18 // Set copy parameters
19let args:CopyObjectArgs = new CopyObjectArgs();
20 // Initiate copying only if the eTag of the source object is the set value
21args.ifMatch = "123456";
22 // Set to “replace” to set userMeta for the target bucket
23args.metadataDirective = "replace";
24 // Set userMeta for the target object
25let userMeta = new TreeMap<string, string>();
26userMeta.set("region", "bj");
27userMeta.set("type", "test");
28args.userMeta = userMeta;
29
30let copyObjectResult: CopyObjectResult;
31try {
32 copyObjectResult = await bosClient.copyObject(destBucketName, destObjectName, srcBucketName, srcObjectName, args);
33 logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
34} catch (bosResponse) {
35 logger.error(`errCode: ${bosResponse.error.code}`)
36 logger.error(`requestId: ${bosResponse.error.requestId}`)
37 logger.error(`errMessage: ${bosResponse.error.message}`)
38 logger.error(`statusCode: ${bosResponse.statusCode}`)
39}
