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

设置桶清单(C SDK)

开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。

功能介绍

桶清单功能可以定期生成桶内对象的元数据信息,通过查看这些信息,可以帮助您更好地了解桶内对象的状态,调用本接口可以配置桶清单功能。

接口约束

  • 您必须是桶拥有者或拥有设置桶清单的权限,才能调用本接口。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutBucketInventoryConfiguration权限,如果使用桶策略则需授予PutBucketInventoryConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略自定义创建桶策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点
  • 每个桶最多可以配置100条清单规则。
  • 清单配置ID在桶内必须唯一。
  • 目标桶必须已存在,且源桶拥有者必须对目标桶拥有写权限。

方法定义

void set_bucket_inventory(const obs_options *options,
    obs_inventory_configuration *inventory_config,
    obs_response_handler *handler, void *callback_data);

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

options

const obs_options*

必选

参数解释:

请求桶的上下文,通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。

约束限制:

取值范围:

默认取值:

inventory_config

表2*

必选

参数解释:

清单配置结构体。

约束限制:

取值范围:

默认取值:

handler

obs_response_handler *

必选

参数解释:

回调结构体,结构体内所有成员都是回调函数的指针,用于设置处理接口响应数据的回调函数。您可以通过设置回调函数,把服务端的响应数据复制到您的自定义回调数据callback_data中。

约束限制:

取值范围:

默认取值:

callback_data

void *

可选

参数解释:

用户自定义回调数据。

约束限制:

取值范围:

默认取值:

表2 obs_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

表5*

可选

参数解释:

清单中包含的可选字段。

约束限制:

取值范围:

默认取值:

is_enabled

bool

必选

参数解释:

是否启用清单规则。

约束限制:

取值范围:

  • true:启用
  • false:禁用

默认取值:

false

表3 obs_inventory_destination

参数名称

参数类型

是否必选

描述

bucket

char *

必选

参数解释:

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

约束限制:

目标桶必须已存在且源桶拥有者对目标桶拥有写权限。

取值范围:

默认取值:

prefix

char *

可选

参数解释:

目标桶中清单文件的前缀。

约束限制:

取值范围:

默认取值:

表4 obs_inventory_filter

参数名称

参数类型

是否必选

描述

prefix

char *

可选

参数解释:

对象前缀,仅对符合该前缀的对象生成清单。

约束限制:

取值范围:

默认取值:

表5 obs_inventory_optional_fields

参数名称

参数类型

是否必选

描述

field_count

unsigned int

必选

参数解释:

可选字段的数量。

约束限制:

取值范围:

默认取值:

fields

char **

必选

参数解释:

可选字段数组。

约束限制:

取值范围:

Size、LastModifiedDate、StorageClass、ETag、IsMultipartUploaded、ReplicationStatus、EncryptionStatus。

默认取值:

代码示例

以下示例展示如何设置桶清单。
#include "eSDKOBS.h"
#include <stdio.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);
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_inventory_destination destination = {0};
    destination.bucket = "example-dest-bucket";
    destination.prefix = "inventory/";
    // 可选字段
    char *fields[] = {"Size", "LastModifiedDate", "StorageClass"};
    obs_inventory_optional_fields optional_fields = {0};
    optional_fields.field_count = 3;
    optional_fields.fields = fields;
    // 清单配置
    obs_inventory_configuration inventory_config = {0};
    inventory_config.id = "inventory1";
    inventory_config.destination = &destination;
    inventory_config.schedule = "Daily";
    inventory_config.format = "CSV";
    inventory_config.included_object_versions = "Current";
    inventory_config.optional_fields = &optional_fields;
    inventory_config.is_enabled = true;
    obs_response_handler response_handler = {&response_properties_callback, &response_complete_callback};
    obs_status ret_status = OBS_STATUS_BUTT;
    set_bucket_inventory(&options, &inventory_config, &response_handler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf("set bucket inventory successfully.\n");
    } else {
        printf("set bucket inventory failed(%s).\n", obs_get_status_name(ret_status));
    }
    obs_deinitialize();
}
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) { *(obs_status*)callback_data = status; }
    if (error && error->message) { printf("Error: %s\n", error->message); }
}

相关链接

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

相关文档