
# 获取桶清单(HarmonyOS SDK)
#### 功能说明
桶清单功能可以定期生成桶内对象的元数据信息，通过查看这些信息，可以帮助您更好地了解桶内对象的状态，可通过本接口获取桶清单。
#### 接口约束
- 您必须是桶拥有者或拥有设置桶清单的权限，才能调用本接口。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:PutBucketInventoryConfiguration权限，如果使用桶策略则需授予PutBucketInventoryConfiguration权限。相关授权方式介绍可参见[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)。
- 每个桶最多可以配置100条清单规则。
- 清单配置ID在桶内必须唯一。
- 目标桶必须已存在，且源桶拥有者必须对目标桶拥有写权限。
 
#### 方法定义
```
getBucketInventory(input: GetBucketInventoryInput): Response<GetBucketInventoryOutput>
```
#### 请求参数
表1请求参数列表 
| **参数名称** | **参数类型**                         | **是否必选** | **描述**                                                                                 |
|:---|:---|:---|:---|
| input    | [表2] | 必选       | **参数解释：** 获取桶清单接口入参，详见[表2]。 |
   
 表2GetBucketInventoryInput 
| **参数名称** | **参数类型** | **是否可选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| Bucket   | string   | 必选       | 桶名。 **约束限制：** - 桶的名字需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。  - 桶命名规则如下： - 3～63个字符，数字或字母开头，支持小写字母、数字、"-"、"."。  - 禁止使用IP地址。  - 禁止以"-"或"."开头及结尾。  - 禁止两个"."相邻（如："my..bucket"）。  - 禁止"."和"-"相邻（如："my-.bucket"和"my.-bucket"）。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **取值范围：** 长度为3～63个字符。 |
| Id       | string   | 必选       | **参数解释：** 清单规则 ID，指定要查询的清单规则。 **默认取值：** 无。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
   
#### 返回结果
表3返回结果 
| **参数类型**                                                                                                                                                     | **描述**                                                                                        |
|:---|:---|
| [Response] 说明： 该接口返回一个Promise类型，需要使用Promise、async/await语法处理。 | **参数解释：** 接口返回信息，详见[Response]。 |
   
 表4Response 
| **参数名称**        | **参数类型**                                       | **描述**                                                                                                                                                                          |
|:---|:---|:---|
| CommonMsg       | [ICommonMsg] | **参数解释：** 接口调用完成后的公共信息，包含HTTP状态码，操作失败的错误码等，详见[ICommonMsg]。                                                        |
| InterfaceResult | [表6]              | **参数解释：** 操作成功后的结果数据，详见[表6]。 **约束限制：** 当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服务端返回的详细错误码。                                                                                                                                                                                                                                                                                              |
   
 表6GetBucketInventoryOutput 
| **参数名称** | **参数类型**                                                       | **描述**                                            |
|:---|:---|:---|
| Rules    | [InventoryConfiguration]\[\] | **参数解释：** 清单规则配置列表。 |
   
 表7InventoryConfiguration 
| **参数名称**               | **参数类型**                                                       | **描述**                                                                                                                            |
|:---|:---|:---|
| Id                     | string                                                         | **参数解释：** 清单规则 ID。 **默认取值：** 无。              |
| IsEnabled              | boolean                                                        | **参数解释：** 是否启用该清单规则。 **默认取值：** 无。           |
| Filter                 | [InventoryFilter]             | **参数解释：** 过滤条件，按对象名前缀筛选导出对象。 **默认取值：** 无。 |
| Schedule               | [InventorySchedule]         | **参数解释：** 清单导出周期。 **默认取值：** 无。          |
| Destination            | [InventoryDestination]       | **参数解释：** 清单导出目标位置。 **默认取值：** 无。       |
| IncludedObjectVersions | [IncludedObjectVersionsType] | **参数解释：** 清单包含的对象版本。 **默认取值：** 无。      |
| OptionalFields         | [OptionalFields]              | **参数解释：** 清单可选字段。 **默认取值：** 无。         |
   
 表8InventoryFilter 
| **参数名称** | **参数类型** | **是否必选** | **描述**                                                                                                                          |
|:---|:---|:---|:---|
| Prefix   | string   | 可选       | **参数解释：** 对象名前缀，只导出匹配该前缀的对象。 **默认取值：** 无。 |
   
 表9InventorySchedule 
| **参数名称**  | **参数类型**                                            | **是否必选** | **描述**                                                                                                                |
|:---|:---|:---|:---|
| Frequency | [FrequencyType] | 必选       | **参数解释：** 清单导出周期。 **默认取值：** 无。 |
   
 表10FrequencyType 
| **常量名** | **原始值** | **说明**    |
|:---|:---|:---|
| DAILY   | Daily   | 每日导出一次清单。 |
| WEEKLY  | Weekly  | 每周导出一次清单。 |
   
 表11InventoryDestination 
| **参数名称** | **参数类型** | **描述**                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|
| Format   | string   | **参数解释：** 生成的清单文件的格式，现只支持CSV格式。 **默认取值：** 无。                                                                                                                                                 |
| Bucket   | string   | **参数解释：** 存放清单文件的目标桶的桶名。 **默认取值：** 无。                                                                                                                                                         |
| Prefix   | string   | **参数解释：** 生成的清单文件对象名会以此前缀开头，如果不配置前缀，则生成的清单文件对象名默认以BucketInventory开头。 **取值范围**： 不包含\\, \*, ?, ", \<, \>, \| 字符，且最长254字符。 **默认取值：** 无 |
   
 表12IncludedObjectVersionsType 
| **常量名** | **原始值** | **说明**        |
|:---|:---|:---|
| ALL     | All     | 清单包含对象的所有版本。  |
| CURRENT | Current | 清单仅包含对象的当前版本。 |
   
 表13OptionalFields 
| **参数名称** | **参数类型**   | **是否必选** | **描述**                                                                                                                      |
|:---|:---|:---|:---|
| Field    | string\[\] | 可选       | **参数解释：** 清单包含的额外字段列表。 **默认取值：** 无。 |
   
#### 代码示例
本示例用于查询 examplebucket 桶下指定规则（ID 为 config1）的清单配置。
```
// 引入依赖包
import ObsClient, { GetBucketInventoryInput } 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 getBucketInventory() {
  try {
    const params: GetBucketInventoryInput = {
        Bucket: "examplebucket",
        Id: "config1" 
    };
    const result = await obsClient.getBucketInventory(params);
    if (result.CommonMsg.Status < 300) {
      hilog.info(0x0001, 'OBS-Harmony', "Get bucket(%s)'s inventory successful!", params.Bucket);
      const rules = result.InterfaceResult?.Rules ?? [];
      for (const r of rules) {
        hilog.info(0x0001, 'OBS-Harmony', "Inventory id: %s, enabled: %s", r.Id, r.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);
  }
}
getBucketInventory();
```
#### 相关链接
- 关于获取桶清单的API说明，请参见[获取桶清单](https://support.huaweicloud.com/api-obs/obs_04_0056.html)。
- 获取桶清单过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
 
