获取桶清单(C SDK)
开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。
功能介绍
桶清单功能可以定期生成桶内对象的元数据信息,通过查看这些信息,可以帮助您更好地了解桶内对象的状态,可通过本接口获取桶清单。
接口约束
- 您必须是桶拥有者或拥有获取桶清单的权限,才能获取桶清单配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketInventoryConfiguration权限,如果使用桶策略则需授予GetBucketInventoryConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。
- OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
方法定义
void get_bucket_inventory(const obs_options *options, const char *inventory_id,
obs_get_bucket_inventory_handler *handler, void *callback_data); 请求参数说明
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| options | const obs_options* | 必选 | 参数解释: 请求桶的上下文,通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 约束限制: 无 取值范围: 无 默认取值: 无 |
| inventory_id | const char * | 必选 | 参数解释: 清单配置ID,用于标识要获取的清单规则。 约束限制: 无 取值范围: 无 默认取值: 无 |
| handler | 必选 | 参数解释: 回调结构体,用于设置处理接口响应数据的回调函数。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| callback_data | void * | 可选 | 参数解释: 用户自定义回调数据。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| response_handler | 必选 | 参数解释: 响应回调函数结构体。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| get_bucket_inventory_callback | 必选 | 参数解释: 清单回调函数指针,可以在这个回调中把inventory_config的内容记录到callback_data中。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| inventory_config | 必选 | 参数解释: 清单配置结构体,包含id、destination、schedule等信息。 约束限制: 该结构体及其中指针指向的内存在回调返回后会被SDK释放,如需保留请自行复制。 取值范围: 无 默认取值: 无 | |
| callback_data | void * | 必选 | 参数解释: 用户自定义回调数据指针。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| id | char * | 必选 | 参数解释: 清单配置ID,在桶内唯一标识一条清单规则。 约束限制: 无 取值范围: 长度为1~64的字符串。 默认取值: 无 |
| destination | 必选 | 参数解释: 目标桶配置,指定清单文件的存放位置。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| schedule | char * | 必选 | 参数解释: 清单生成频率。 约束限制: 无 取值范围:
默认取值: 无 |
| format | char * | 必选 | 参数解释: 清单文件格式。 约束限制: 无 取值范围: CSV。 默认取值: 无 |
| included_object_versions | char * | 必选 | 参数解释: 清单中包含的对象版本。 约束限制: 无 取值范围:
默认取值: 无 |
| filter | 可选 | 参数解释: 过滤器,仅对符合前缀条件的对象生成清单。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| optional_fields | 可选 | 参数解释: 清单中包含的可选字段。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| is_enabled | bool | 必选 | 参数解释: 是否启用清单规则。 约束限制: 无 取值范围:
默认取值: false |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| bucket | char * | 必选 | 参数解释: 存放清单文件的目标桶名称。 约束限制: 目标桶必须已存在且源桶拥有者对目标桶拥有写权限。 取值范围: 无 默认取值: 无 |
| prefix | char * | 可选 | 参数解释: 目标桶中清单文件的前缀。 约束限制: 无 取值范围: 无 默认取值: 无 |
代码示例
以下示例展示如何获取桶清单配置。
#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 get_inventory_callback(obs_inventory_configuration *inventory_config, void *callback_data);
typedef struct InventoryInfo {
obs_status ret_status;
} InventoryInfo;
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_get_bucket_inventory_handler get_inventory_handler = {
response_handler,
&get_inventory_callback
};
InventoryInfo inventory_info = {0};
inventory_info.ret_status = OBS_STATUS_BUTT;
get_bucket_inventory(&options, "inventory1", &get_inventory_handler, &inventory_info);
if (OBS_STATUS_OK == inventory_info.ret_status) {
printf("get bucket inventory successfully.\n");
} else {
printf("get bucket inventory failed(%s).\n", obs_get_status_name(inventory_info.ret_status));
}
obs_deinitialize();
}
obs_status get_inventory_callback(obs_inventory_configuration *inventory_config, void *callback_data)
{
if (inventory_config) {
printf("id: %s\n", inventory_config->id ? inventory_config->id : "");
printf("schedule: %s\n", inventory_config->schedule ? inventory_config->schedule : "");
printf("format: %s\n", inventory_config->format ? inventory_config->format : "");
printf("is_enabled: %s\n", inventory_config->is_enabled ? "true" : "false");
if (inventory_config->destination && inventory_config->destination->bucket) {
printf("destination bucket: %s\n", inventory_config->destination->bucket);
}
}
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) { ((InventoryInfo*)callback_data)->ret_status = status; }
if (error && error->message) { printf("Error: %s\n", error->message); }
}