更新时间:2026-08-28 GMT+08:00
分享

列举桶清单(HarmonyOS SDK)

功能说明

调用列举桶清单接口,您可获取指定桶的所有清单配置信息。

接口约束

  • 您必须是桶拥有者或拥有列举桶清单的权限,才能列举桶清单配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketInventoryConfiguration权限,如果使用桶策略则需授予GetBucketInventoryConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略自定义创建桶策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点

方法定义

1
listBucketInventory(input: ListBucketInventoryInput): Response<ListBucketInventoryOutput>

请求参数

表1 请求参数列表

参数名称

参数类型

是否必选

描述

input

ListBucketInventoryInput

必选

参数解释:

列举桶清单接口入参,详见ListBucketInventoryInput

表2 ListBucketInventoryInput

参数名称

参数类型

是否可选

描述

Bucket

string

必选

桶名。

约束限制:

  • 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。
  • 桶命名规则如下:
    • 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。
    • 禁止使用IP地址。
    • 禁止以“-”或“.”开头及结尾。
    • 禁止两个“.”相邻(如:“my..bucket”)。
    • 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。
  • 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。

取值范围:

长度为3~63个字符。

返回结果

表3 返回结果

参数类型

描述

Response

说明:

该接口返回一个Promise类型,需要使用Promise、async/await语法处理。

参数解释:

接口返回信息,详见Response

表4 Response

参数名称

参数类型

描述

CommonMsg

ICommonMsg

参数解释:

接口调用完成后的公共信息,包含HTTP状态码,操作失败的错误码等,详见ICommonMsg

InterfaceResult

ListBucketInventoryOutput

参数解释:

操作成功后的结果数据,详见ListBucketInventoryOutput

约束限制:

当Status大于300时为空。

表5 ICommonMsg

参数名称

参数类型

描述

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服务端返回的详细错误码。

表6 ListBucketInventoryOutput

参数名称

参数类型

描述

IsTruncated

boolean

参数解释:

本次列举是否被截断。true表示结果不完整,需结合NextInventoryMarker继续列举。

NextInventoryMarker

string

参数解释:下次列举的起始位置,当IsTruncated为true时返回。

Rules

InventoryConfiguration[]

参数解释:

桶下全部清单规则列表。每项为一条清单规则,含规则ID、是否启用、过滤条件、清单周期、目标位置等字段。

表7 InventoryConfiguration

参数名称

参数类型

描述

Id

string

参数解释:

清单规则 ID。

默认取值:

无。

IsEnabled

boolean

参数解释:

是否启用该清单规则。

默认取值:

无。

Filter

InventoryFilter

参数解释:

过滤条件,按对象名前缀筛选导出对象。

默认取值:

无。

Schedule

InventorySchedule

参数解释:

清单导出周期。

默认取值:

无。

Destination

InventoryDestination

参数解释:

清单导出目标位置。

默认取值:

无。

IncludedObjectVersions

IncludedObjectVersionsType

参数解释:

清单包含的对象版本。

默认取值:

无。

OptionalFields

OptionalFields

参数解释:

清单可选字段。

默认取值:

无。

表8 InventoryFilter

参数名称

参数类型

是否必选

描述

Prefix

string

可选

参数解释:

对象名前缀,只导出匹配该前缀的对象。

默认取值:

无。

表9 InventorySchedule

参数名称

参数类型

是否必选

描述

Frequency

表6 FrequencyType

必选

参数解释:

清单导出周期。

取值范围:

详见表6 FrequencyType

默认取值:

无。

表10 FrequencyType

常量名

原始值

说明

DAILY

Daily

每日导出一次清单。

WEEKLY

Weekly

每周导出一次清单。

表11 InventoryDestination

参数名称

参数类型

描述

Format

string

参数解释:

生成的清单文件的格式,现只支持CSV格式。

默认取值:

无。

Bucket

string

参数解释:

存放清单文件的目标桶的桶名。

默认取值:

无。

Prefix

string

参数解释:

生成的清单文件对象名会以此前缀开头,如果不配置前缀,则生成的清单文件对象名默认以BucketInventory开头。

取值范围

不包含\, *, ?, ", <, >, | 字符,且最长254字符。

默认取值:

表12 IncludedObjectVersionsType

常量名

原始值

说明

ALL

All

清单包含对象的所有版本。

CURRENT

Current

清单仅包含对象的当前版本。

表13 OptionalFields

参数名称

参数类型

是否必选

描述

Field

string[]

可选

参数解释:

清单包含的额外字段列表。

默认取值:

无。

代码示例

本示例用于列举名为 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
// 引入依赖包
import ObsClient, { ListBucketInventoryInput } from '@obs/esdk-obs-harmony';
import { process } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const processManager = new process.ProcessManager();

// 创建ObsClient实例
const obsClient = new ObsClient({
  // 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险
  // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
  AccessKeyId: processManager.getEnvironmentVar("ACCESS_KEY_ID"),
  SecretAccessKey: processManager.getEnvironmentVar("SECRET_ACCESS_KEY"),
  // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入
  // SecurityToken: processManager.getEnvironmentVar("SECURITY_TOKEN"),
  // Server填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写
  Server: "https://obs.cn-north-4.myhuaweicloud.com",
});

async function listBucketInventory() {
  try {
    const params: ListBucketInventoryInput = { Bucket: "examplebucket" };
    const result = await obsClient.listBucketInventory(params);
    if (result.CommonMsg.Status < 300) {
      hilog.info(0x0001, 'OBS-Harmony', "List bucket(%s)'s inventory successful!", params.Bucket);
      const rules = result.InterfaceResult?.Rules ?? [];
      for (const rule of rules) {
        hilog.info(0x0001, 'OBS-Harmony', "Inventory rule id: %s, enabled: %s", rule.Id, rule.IsEnabled);
      }
      return;
    }
    hilog.error(0x0001, 'OBS-Harmony', "An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    hilog.error(0x0001, 'OBS-Harmony', "Status: %d", result.CommonMsg.Status);
    hilog.error(0x0001, 'OBS-Harmony', "Code: %s", result.CommonMsg.Code);
    hilog.error(0x0001, 'OBS-Harmony', "Message: %s", result.CommonMsg.Message);
    hilog.error(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    const err = error as BusinessError;
    hilog.error(0x0001, 'OBS-Harmony', 'An Exception occurred: code=%d, message: %s', err.code, err.message);
  }
}

listBucketInventory();

相关链接

  • 列举桶清单过程中返回的错误码含义、问题原因及处理措施可参考OBS错误码

相关文档