
# 列举桶清单(C SDK)
![](https://support.huaweicloud.com/sdk-c-devg-obs/public_sys-resources/notice_3.0-zh-cn.png)
开发过程中，您有任何问题可以在GitHub上[提交issue](https://github.com/huaweicloud/huaweicloud-sdk-c-obs/issues)，或者在[华为云对象存储服务论坛](https://bbs.huaweicloud.com/forum/forum-620-1.html)中发帖求助。
#### 功能说明
调用列举桶清单接口，您可获取指定桶的所有清单配置信息。
#### 接口约束
- 您必须是桶拥有者或拥有列举桶清单的权限，才能列举桶清单配置。建议使用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)。
 
#### 方法定义
```
void list_bucket_inventory(const obs_options *options,
    obs_list_bucket_inventory_handler *handler, void *callback_data);
```
#### 请求参数说明
表1请求参数列表 
| 参数名称          | 参数类型                                                                                                                | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                             |
|:---|:---|:---|:---|
| options       | const [obs_options](https://support.huaweicloud.com/sdk-c-devg-obs/obs_20_0085.html#obs_20_0085__table9681927859)\* | 必选   | **参数解释：** 请求桶的上下文，通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
| handler       | [表2 obs_list_bucket_inventory_handler]\*                                         | 必选   | **参数解释：** 回调结构体，用于设置处理接口响应数据的回调函数。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                          |
| callback_data | void \*                                                                                                             | 可选   | **参数解释：** 用户自定义回调数据。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                          |
   
 表2obs_list_bucket_inventory_handler 
| 参数名称                           | 参数类型                                                                                                               | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| response_handler               | [obs_response_handler](https://support.huaweicloud.com/sdk-c-devg-obs/obs_20_0087.html#obs_20_0087__table53755070) | 必选   | **参数解释：** 响应回调函数结构体。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                |
| list_bucket_inventory_callback | [表3 obs_list_bucket_inventory_callback]\*                                       | 必选   | **参数解释：** 清单列表回调函数指针，可以在这个回调中把inventory_list的内容记录到callback_data中。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
   
 表3obs_list_bucket_inventory_callback 
| 参数名称            | 参数类型                                                                  | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| inventory_list  | [表4 obs_inventory_configuration]\*\* | 必选   | **参数解释：** 清单配置数组，数组中每个元素指向一条清单配置。 **约束限制：** 该数组及其中指针指向的内存在回调返回后会被SDK释放，如需保留请自行复制。 **取值范围：** 无 **默认取值：** 无 |
| inventory_count | unsigned int                                                          | 必选   | **参数解释：** 清单配置的数量。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                   |
| callback_data   | void \*                                                               | 必选   | **参数解释：** 用户自定义回调数据指针。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                           |
   
 表4obs_inventory_configuration 
| 参数名称                     | 参数类型                                                              | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|:---|:---|:---|:---|
| id                       | char \*                                                           | 必选   | **参数解释：** 清单配置ID，在桶内唯一标识一条清单规则。 **约束限制：** 无 **取值范围：** 长度为1\~64的字符串。 **默认取值：** 无                                                                                                                                               |
| destination              | [表3 obs_inventory_destination]\* | 必选   | **参数解释：** 目标桶配置，指定清单文件的存放位置。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                          |
| schedule                 | char \*                                                           | 必选   | **参数解释：** 清单生成频率。 **约束限制：** 无 **取值范围：** - Daily：每天  - Weekly：每周   **默认取值：** 无                  |
| format                   | char \*                                                           | 必选   | **参数解释：** 清单文件格式。 **约束限制：** 无 **取值范围：** CSV。 **默认取值：** 无                                                                                                                                                                  |
| included_object_versions | char \*                                                           | 必选   | **参数解释：** 清单中包含的对象版本。 **约束限制：** 无 **取值范围：** - All：包含所有版本  - Current：仅包含当前版本   **默认取值：** 无 |
| filter                   | [表4 obs_inventory_filter]\*      | 可选   | **参数解释：** 过滤器，仅对符合前缀条件的对象生成清单。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                        |
| optional_fields          | [表7]\*                            | 可选   | **参数解释：** 清单中包含的可选字段。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                                       |
| is_enabled               | bool                                                              | 必选   | **参数解释：** 是否启用清单规则。 **约束限制：** 无 **取值范围：** - true：启用  - false：禁用   **默认取值：** false               |
   
 表5obs_inventory_destination 
| 参数名称   | 参数类型    | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|:---|
| bucket | char \* | 必选   | 参数解释： 存放清单文件的目标桶名称。 约束限制： 目标桶必须已存在且源桶拥有者对目标桶拥有写权限。 取值范围： 无 默认取值： 无 |
| prefix | char \* | 可选   | 参数解释： 目标桶中清单文件的前缀。 约束限制： 无 取值范围： 无 默认取值： 无                   |
   
 表6obs_inventory_filter 
| 参数名称   | 参数类型    | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| prefix | char \* | 可选   | 参数解释： 对象前缀，仅对符合该前缀的对象生成清单。 约束限制： **无** 取值范围： **无** 默认取值： 无 |
   
 表7obs_inventory_optional_fields 
| 参数名称        | 参数类型         | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| field_count | unsigned int | 必选   | 参数解释： 可选字段的数量。 约束限制： 无 取值范围： 无 默认取值： 无                                                                                                    |
| fields      | char \*\*    | 必选   | 参数解释： 可选字段数组。 约束限制： 无 取值范围： Size、LastModifiedDate、StorageClass、ETag、IsMultipartUploaded、ReplicationStatus、EncryptionStatus。 默认取值： 无 |
   
#### 代码示例
以下示例展示如何列举桶清单配置。
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <string.h>
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
obs_status list_inventory_callback(obs_inventory_configuration **inventory_list,
    unsigned int inventory_count, void *callback_data);
typedef struct InventoryListInfo {
    unsigned int inventory_count;
    obs_status ret_status;
} InventoryListInfo;
int main()
{
    obs_initialize(OBS_INIT_ALL);
    obs_options options;
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    options.bucket_options.bucket_name = "example-source-bucket";
    obs_response_handler response_handler = {&response_properties_callback, &response_complete_callback};
    obs_list_bucket_inventory_handler list_inventory_handler = {
        response_handler,
        &list_inventory_callback
    };
    InventoryListInfo info = {0};
    info.ret_status = OBS_STATUS_BUTT;
    list_bucket_inventory(&options, &list_inventory_handler, &info);
    if (OBS_STATUS_OK == info.ret_status) {
        printf("list bucket inventory successfully. count: %u\n", info.inventory_count);
    } else {
        printf("list bucket inventory failed(%s).\n", obs_get_status_name(info.ret_status));
    }
    obs_deinitialize();
}
obs_status list_inventory_callback(obs_inventory_configuration **inventory_list,
    unsigned int inventory_count, void *callback_data)
{
    InventoryListInfo *info = (InventoryListInfo *)callback_data;
    info->inventory_count = inventory_count;
    unsigned int i;
    for (i = 0; i < inventory_count; i++) {
        obs_inventory_configuration *config = inventory_list[i];
        if (config) {
            printf("inventory[%u]: id=%s, schedule=%s, enabled=%s\n",
                i,
                config->id ? config->id : "",
                config->schedule ? config->schedule : "",
                config->is_enabled ? "true" : "false");
        }
    }
    return OBS_STATUS_OK;
}
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    (void)properties; (void)callback_data;
    return OBS_STATUS_OK;
}
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) { ((InventoryListInfo*)callback_data)->ret_status = status; }
    if (error && error->message) { printf("Error: %s\n", error->message); }
}
```
#### 相关链接
- 关于列举桶清单的API说明，请参见[列举桶清单](https://support.huaweicloud.com/api-obs/obs_04_0057.html)。

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