
# 列举桶清单(HarmonyOS SDK)
#### 功能说明
调用列举桶清单接口，您可获取指定桶的所有清单配置信息。
#### 接口约束
- 您必须是桶拥有者或拥有列举桶清单的权限，才能列举桶清单配置。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:GetBucketInventoryConfiguration权限，如果使用桶策略则需授予GetBucketInventoryConfiguration权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/OBS)。
 
#### 方法定义
```
listBucketInventory(input: ListBucketInventoryInput): Response<ListBucketInventoryOutput>
```
#### 请求参数
表1请求参数列表 
| **参数名称** | **参数类型**                                               | **是否必选** | **描述**                                                                                                       |
|:---|:---|:---|:---|
| input    | [ListBucketInventoryInput] | 必选       | **参数解释：** 列举桶清单接口入参，详见[ListBucketInventoryInput]。 |
   
 表2ListBucketInventoryInput 
| **参数名称** | **参数类型** | **是否可选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| Bucket   | string   | 必选       | 桶名。 **约束限制：** - 桶的名字需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。  - 桶命名规则如下： - 3～63个字符，数字或字母开头，支持小写字母、数字、"-"、"."。  - 禁止使用IP地址。  - 禁止以"-"或"."开头及结尾。  - 禁止两个"."相邻（如："my..bucket"）。  - 禁止"."和"-"相邻（如："my-.bucket"和"my.-bucket"）。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **取值范围：** 长度为3～63个字符。 |
   
#### 返回结果
表3返回结果 
| **参数类型**                                                                                                                                                     | **描述**                                                                                        |
|:---|:---|
| [Response] 说明： 该接口返回一个Promise类型，需要使用Promise、async/await语法处理。 | **参数解释：** 接口返回信息，详见[Response]。 |
   
 表4Response 
| **参数名称**        | **参数类型**                                                 | **描述**                                                                                                                                                                                                 |
|:---|:---|:---|
| CommonMsg       | [ICommonMsg]           | **参数解释：** 接口调用完成后的公共信息，包含HTTP状态码，操作失败的错误码等，详见[ICommonMsg]。                                                                               |
| InterfaceResult | [ListBucketInventoryOutput] | **参数解释：** 操作成功后的结果数据，详见[ListBucketInventoryOutput]。 **约束限制：** 当Status大于300时为空。 |
   
 表5ICommonMsg 
| **参数名称**  | **参数类型** | **描述**                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| Status    | number   | **参数解释：** OBS服务端返回的HTTP状态码。 **取值范围：** 状态码是一组从2xx（成功）到4xx或5xx（错误）的数字代码，状态码表示了请求响应的状态。完整的状态码列表请参见[状态码](https://support.huaweicloud.com/api-obs/obs_04_0114.html)。 |
| Code      | string   | **参数解释：** OBS服务端返回的错误码。                                                                                                                                                                                                                                                                                                  |
| Message   | string   | **参数解释：** OBS服务端返回的错误描述。                                                                                                                                                                                                                                                                                                |
| HostId    | string   | **参数解释：** OBS服务端返回的请求服务端ID。                                                                                                                                                                                                                                                                                             |
| RequestId | string   | **参数解释：** OBS服务端返回的请求ID。                                                                                                                                                                                                                                                                                                |
| Id2       | string   | **参数解释：** OBS服务端返回的请求ID2。                                                                                                                                                                                                                                                                                             |
| Indicator | string   | **参数解释：** OBS服务端返回的详细错误码。                                                                                                                                                                                                                                                                                              |
   
 表6ListBucketInventoryOutput 
| **参数名称**            | **参数类型**                                                       | **描述**                                                                                      |
|:---|:---|:---|
| IsTruncated         | boolean                                                        | **参数解释：** 本次列举是否被截断。true表示结果不完整，需结合NextInventoryMarker继续列举。 |
| NextInventoryMarker | string                                                         | **参数解释：**下次列举的起始位置，当IsTruncated为true时返回。                                                    |
| Rules               | [InventoryConfiguration]\[\] | **参数解释：** 桶下全部清单规则列表。每项为一条清单规则，含规则ID、是否启用、过滤条件、清单周期、目标位置等字段。   |
   
 表7InventoryConfiguration 
| **参数名称**               | **参数类型**                                                       | **描述**                                                                                                                            |
|:---|:---|:---|
| Id                     | string                                                         | **参数解释：** 清单规则 ID。 **默认取值：** 无。              |
| IsEnabled              | boolean                                                        | **参数解释：** 是否启用该清单规则。 **默认取值：** 无。           |
| Filter                 | [InventoryFilter]             | **参数解释：** 过滤条件，按对象名前缀筛选导出对象。 **默认取值：** 无。 |
| Schedule               | [InventorySchedule]         | **参数解释：** 清单导出周期。 **默认取值：** 无。          |
| Destination            | [InventoryDestination]       | **参数解释：** 清单导出目标位置。 **默认取值：** 无。       |
| IncludedObjectVersions | [IncludedObjectVersionsType] | **参数解释：** 清单包含的对象版本。 **默认取值：** 无。      |
| OptionalFields         | [OptionalFields]              | **参数解释：** 清单可选字段。 **默认取值：** 无。         |
   
 表8InventoryFilter 
| **参数名称** | **参数类型** | **是否必选** | **描述**                                                                                                                          |
|:---|:---|:---|:---|
| Prefix   | string   | 可选       | **参数解释：** 对象名前缀，只导出匹配该前缀的对象。 **默认取值：** 无。 |
   
 表9InventorySchedule 
| **参数名称**  | **参数类型**                                                                                                                    | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| Frequency | [表6 FrequencyType](https://support.huaweicloud.com/sdk-harmony-devg-obs/obs_34_0715.html#obs_34_0715__table108361541154918) | 必选       | **参数解释：** 清单导出周期。 **取值范围：** 详见[表6 FrequencyType](https://support.huaweicloud.com/sdk-harmony-devg-obs/obs_34_0714.html#obs_34_0714__table108361541154918)。 **默认取值：** 无。 |
   
表10FrequencyType 
| **常量名** | **原始值** | **说明**    |
|:---|:---|:---|
| DAILY   | Daily   | 每日导出一次清单。 |
| WEEKLY  | Weekly  | 每周导出一次清单。 |
   
 表11InventoryDestination 
| **参数名称** | **参数类型** | **描述**                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|
| Format   | string   | **参数解释：** 生成的清单文件的格式，现只支持CSV格式。 **默认取值：** 无。                                                                                                                                                 |
| Bucket   | string   | **参数解释：** 存放清单文件的目标桶的桶名。 **默认取值：** 无。                                                                                                                                                         |
| Prefix   | string   | **参数解释：** 生成的清单文件对象名会以此前缀开头，如果不配置前缀，则生成的清单文件对象名默认以BucketInventory开头。 **取值范围**： 不包含\\, \*, ?, ", \<, \>, \| 字符，且最长254字符。 **默认取值：** 无 |
   
 表12IncludedObjectVersionsType 
| **常量名** | **原始值** | **说明**        |
|:---|:---|:---|
| ALL     | All     | 清单包含对象的所有版本。  |
| CURRENT | Current | 清单仅包含对象的当前版本。 |
   
 表13OptionalFields 
| **参数名称** | **参数类型**   | **是否必选** | **描述**                                                                                                                      |
|:---|:---|:---|:---|
| Field    | string\[\] | 可选       | **参数解释：** 清单包含的额外字段列表。 **默认取值：** 无。 |
   
#### 代码示例
本示例用于列举名为 examplebucket 的桶下全部清单规则。
```
// 引入依赖包
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();
```
#### 相关链接
- 关于列举桶清单的API说明，请参见[列举桶清单](https://support.huaweicloud.com/api-obs/obs_04_0057.html)。

- 列举桶清单过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
 
