更新时间:2026-01-15 GMT+08:00
删除桶(Node.js SDK)
功能说明
桶为空时,用户可以删除桶,以免占用桶数量配额。删除桶后需要等待30分钟才能创建同名桶。
接口约束
- 待删除的桶必须为空,桶为空包含两方面含义:
- 桶内没有任何对象,没有对象的任何历史版本,没有对象的删除标记(删除标记也视作一个历史版本)。
- 桶内没有任何未合并的多段上传任务,即桶内不存在碎片。
- 删除桶非幂等操作,删除不存在的桶会失败。
- 您必须是桶拥有者或拥有删除桶的权限,才能删除桶。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:DeleteBucket权限,如果使用桶策略则需授予DeleteBucket权限。
方法定义
ObsClient.deleteBucket(params)
请求参数
参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
Bucket | string | 必选 | 参数解释: 桶名。 约束限制:
取值范围: 长度为3~63个字符。 默认取值: 无 |
返回结果
参数名称 | 参数类型 | 描述 |
|---|---|---|
CommonMsg | 参数解释: 接口调用完成后的公共信息,包含HTTP状态码,操作失败的错误码等,详见ICommonMsg。 | |
InterfaceResult | 参数解释: 操作成功后的结果数据,详见BaseResponseOuput。 约束限制: 当Status大于300时为空。 |
参数名称 | 参数类型 | 描述 |
Status | number | 参数解释: OBS服务端返回的HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。 |
Code | string | 参数解释: OBS服务端返回的错误码。 |
Message | string | 参数解释: OBS服务端返回的错误描述。 |
HostId | string | 参数解释: OBS服务端返回的请求服务端ID。 |
RequestId | string | 参数解释: OBS服务端返回的请求ID。 |
Id2 | string | 参数解释: OBS服务端返回的请求ID2。 |
Indicator | string | 参数解释: OBS服务端返回的详细错误码。 |
代码示例
您可以通过ObsClient.deleteBucket删除桶。以下代码展示如何删除名为examplebucket的桶。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // 引入obs库 // 使用npm安装 const ObsClient = require("esdk-obs-nodejs"); // 使用源码安装 // var ObsClient = require('./lib/obs'); // 创建ObsClient实例 const obsClient = new ObsClient({ // 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险 // 您可以登录访问管理控制台获取访问密钥AK/SK access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入 // security_token: process.env.SECURITY_TOKEN, // endpoint填写桶所在区域的endpoint server: "https://your-endpoint" }); async function deleteBucket() { try { const params = { // 指定存储桶名称 Bucket: "examplebucket" }; // 删除桶 const result = await obsClient.deleteBucket(params); if (result.CommonMsg.Status <= 300) { console.log("Delete bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; deleteBucket(); |
父主题:管理桶(Node.js SDK)

