列举桶清单(C SDK)
开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。
功能介绍
调用列举桶清单接口,您可获取指定桶的所有清单配置信息。
接口约束
- 您必须是桶拥有者或拥有列举桶清单的权限,才能列举桶清单配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketInventoryConfiguration权限,如果使用桶策略则需授予GetBucketInventoryConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。
- OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
方法定义
void list_bucket_inventory(const obs_options *options,
obs_list_bucket_inventory_handler *handler, void *callback_data); 请求参数说明
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| options | const obs_options* | 必选 | 参数解释: 请求桶的上下文,通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 约束限制: 无 取值范围: 无 默认取值: 无 |
| handler | 必选 | 参数解释: 回调结构体,用于设置处理接口响应数据的回调函数。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| callback_data | void * | 可选 | 参数解释: 用户自定义回调数据。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| response_handler | 必选 | 参数解释: 响应回调函数结构体。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| list_bucket_inventory_callback | 必选 | 参数解释: 清单列表回调函数指针,可以在这个回调中把inventory_list的内容记录到callback_data中。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| inventory_list | 必选 | 参数解释: 清单配置数组,数组中每个元素指向一条清单配置。 约束限制: 该数组及其中指针指向的内存在回调返回后会被SDK释放,如需保留请自行复制。 取值范围: 无 默认取值: 无 | |
| inventory_count | unsigned int | 必选 | 参数解释: 清单配置的数量。 约束限制: 无 取值范围: 无 默认取值: 无 |
| callback_data | void * | 必选 | 参数解释: 用户自定义回调数据指针。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| id | char * | 必选 | 参数解释: 清单配置ID,在桶内唯一标识一条清单规则。 约束限制: 无 取值范围: 长度为1~64的字符串。 默认取值: 无 |
| destination | 必选 | 参数解释: 目标桶配置,指定清单文件的存放位置。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| schedule | char * | 必选 | 参数解释: 清单生成频率。 约束限制: 无 取值范围:
默认取值: 无 |
| format | char * | 必选 | 参数解释: 清单文件格式。 约束限制: 无 取值范围: CSV。 默认取值: 无 |
| included_object_versions | char * | 必选 | 参数解释: 清单中包含的对象版本。 约束限制: 无 取值范围:
默认取值: 无 |
| filter | 可选 | 参数解释: 过滤器,仅对符合前缀条件的对象生成清单。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| optional_fields | 表7* | 可选 | 参数解释: 清单中包含的可选字段。 约束限制: 无 取值范围: 无 默认取值: 无 |
| is_enabled | bool | 必选 | 参数解释: 是否启用清单规则。 约束限制: 无 取值范围:
默认取值: false |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| bucket | char * | 必选 | 参数解释: 存放清单文件的目标桶名称。 约束限制: 目标桶必须已存在且源桶拥有者对目标桶拥有写权限。 取值范围: 无 默认取值: 无 |
| prefix | char * | 可选 | 参数解释: 目标桶中清单文件的前缀。 约束限制: 无 取值范围: 无 默认取值: 无 |
代码示例
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #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); } } |