拷贝Object
更新时间:2025-04-01
简单拷贝Object
基本流程
- 创建BosClient 类的实例。
- 执行BosClient.copyObject()方法。
- 返回CopyObjectResult类实例,可通过
lastModifie
和eTag
成员查看最后修改时间和eTag.
示例代码
let copyObjectResult: CopyObjectResult;
try {
let destObjectName = objectName + ".cp"; // copy目的object名称
copyObjectResult = await bosClient.copyObject(bucketName, destObjectName, bucketName, objectName);
logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
} catch (bosResponse) {
logger.error(`errCode: ${bosResponse.error.code}`)
logger.error(`requestId: ${bosResponse.error.requestId}`)
logger.error(`errMessage: ${bosResponse.error.message}`)
logger.error(`statusCode: ${bosResponse.statusCode}`)
}
完整示例
import { logger, Credential, BosClient, ClientOptions } from "bos"
import { CopyObjectResult } from "bos/src/main/ets/bos/api/DataType"
let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //STS返回的临时AK/SK及Token
let clientOptions = new ClientOptions();
clientOptions.endpoint = "bj.bcebos.com"; //传入Bucket所在区域域名
let bosClient = new BosClient(credential, clientOptions); // 创建BosClient
let bucketName = "test-harmony-bucket";
let objectName = "test-object"; // copy源object名称
let copyObjectResult: CopyObjectResult;
try {
let destObjectName = objectName + ".cp"; // copy目的object名称
copyObjectResult = await bosClient.copyObject(bucketName, destObjectName, bucketName, objectName);
logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
} catch (bosResponse) {
logger.error(`errCode: ${bosResponse.error.code}`)
logger.error(`requestId: ${bosResponse.error.requestId}`)
logger.error(`errMessage: ${bosResponse.error.message}`)
logger.error(`statusCode: ${bosResponse.statusCode}`)
}
通过CopyObjectArgs拷贝Object
您也可以通过 CopyObjectArgs
实现object的拷贝。该功能一般用于如下场景:
- 拷贝一个Object但重新设置meta。
- 重置某个现有Object的meta(把源Object和目的Object设置为同一个Object)。
基本流程
- 创建CopyObjectArgs类的实例,设置
metadataDirective
/ifMatch
/userMeta
等参数,含义可见copyObject参数设置。 - 调用copyObject方法,传入CopyObjectArgs类的实例
示例代码
import TreeMap from '@ohos.util.TreeMap';
import { logger, Credential, BosClient, ClientOptions } from "bos"
import { CopyObjectResult, CopyObjectArgs } from "bos/src/main/ets/bos/api/DataType"
let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //STS返回的临时AK/SK及Token
let clientOptions = new ClientOptions();
clientOptions.endpoint = "bj.bcebos.com"; //传入Bucket所在区域域名
let bosClient = new BosClient(credential, clientOptions); // 创建BosClient
//拷贝的目的bucket和目的object
let destBucketName = "dest-bucket";
let destObjectName = "dest-object";
//拷贝的源bucket和源object
let srcBucketName = "src-bucket"
let srcObjectName = "src-object";
//设置拷贝的参数
let args:CopyObjectArgs = new CopyObjectArgs();
//如果源object的eTag是设定的值,则发起拷贝
args.ifMatch = "123456";
//设置为replace才可对目的bucket设置userMeta
args.metadataDirective = "replace";
//设置目的object的userMeta
let userMeta = new TreeMap<string, string>();
userMeta.set("region", "bj");
userMeta.set("type", "test");
args.userMeta = userMeta;
let copyObjectResult: CopyObjectResult;
try {
copyObjectResult = await bosClient.copyObject(destBucketName, destObjectName, srcBucketName, srcObjectName, args);
logger.info(`Copy object success, return : ${JSON.stringify(copyObjectResult)}`);
} catch (bosResponse) {
logger.error(`errCode: ${bosResponse.error.code}`)
logger.error(`requestId: ${bosResponse.error.requestId}`)
logger.error(`errMessage: ${bosResponse.error.message}`)
logger.error(`statusCode: ${bosResponse.statusCode}`)
}