删除Object
更新时间:2025-04-01
基本流程
- 创建BosClient类的实例。
- 执行BosClient.deleteObject()方法。
- 若操作失败后抛出异常,若成功无返回值。
示例代码
// delete single object
try {
await bosClient.deleteObject(bucketName, objectName);
logger.info(`delete object success`);
} 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"
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";
// delete single object
try {
await bosClient.deleteObject(bucketName, objectName);
logger.info(`delete object success`);
} 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}`)
}