获取桶级阻止公共访问配置(C SDK)
开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。
功能说明
可通过本接口获取桶级BPA配置的相关信息。
接口约束
- 您必须是桶拥有者或拥有获取桶级阻止公共访问配置的权限,才能获取桶级阻止公共访问配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketPublicAccessBlock权限,如果使用桶策略则需授予GetBucketPublicAccessBlock权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。
- OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
方法定义
void get_bucket_public_access_block(const obs_options *options,
obs_get_bucket_public_access_block_handler *handler, void *callback_data); 请求参数说明
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| options | const obs_options* | 必选 | 参数解释: 请求桶的上下文,通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 约束限制: 无 取值范围: 无 默认取值: 无 |
| handler | 必选 | 参数解释: 回调结构体,包含响应回调、结束回调和获取阻止公共访问配置的回调。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| callback_data | void * | 可选 | 参数解释: 用户自定义回调数据。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| response_handler | 必选 | 参数解释: 响应回调结构体,包含properties_callback和complete_callback。 约束限制: 无 取值范围: 无 默认取值: 无 | |
| get_bpa_callback | 必选 | 参数解释: 获取阻止公共访问配置的回调函数指针,在该回调中可以读取obs_bucket_public_access_block的内容。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| public_access_block | 必选 | 参数解释: 桶级阻止公共访问配置结构体,包含四个保护开关的状态。 约束限制: 该结构体及其中指针指向的内存在回调返回后会被SDK释放,如需保留请自行复制。 取值范围: 无 默认取值: 无 | |
| callback_data | void * | 必选 | 参数解释: 用户自定义回调数据指针。 约束限制: 无 取值范围: 无 默认取值: 无 |
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| block_public_acls | bool | 参数解释: 是否锁定公共ACL。当为true时,禁止对象上传和ACL修改API设置公共ACL。 约束限制: 无 取值范围:
默认取值: false |
| ignore_public_acls | bool | 参数解释: 是否忽略公共ACL。当为true时,OBS进行权限检查时公共ACL不生效。 约束限制: 无 取值范围:
默认取值: false |
| block_public_policy | bool | 参数解释: 是否锁定公共策略。当为true时,桶策略修改API禁止设置公共策略。 约束限制: 无 取值范围:
默认取值: false |
| restrict_public_buckets | bool | 参数解释: 是否限制账号访问。当为true时,如果桶策略状态为公开,则只允许云服务账号和本账号访问。 约束限制: 无 取值范围:
默认取值: false |
代码示例
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数,可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
// 获取桶级阻止公共访问配置的数据回调函数
obs_status get_bpa_callback(const obs_bucket_public_access_block *public_access_block, void *callback_data);
// 自定义回调数据:用于在回调间传递结果状态和配置内容
typedef struct BpaResult
{
obs_bucket_public_access_block bpa_config;
obs_status ret_status;
} BpaResult;
int main()
{
// 以下示例展示如何获取桶级阻止公共访问配置:
// 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
obs_status ret_status = obs_initialize(OBS_INIT_ALL);
if (OBS_STATUS_OK != ret_status)
{
printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
return -1;
}
obs_options options;
// 创建并初始化options,该参数包括访问域名(host_name)、访问密钥(access_key_id和access_key_secret)、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
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");
// 填写Bucket名称,例如example-bucket-name。
char * bucket_name = "example-bucket-name";
options.bucket_options.bucket_name = bucket_name;
// 设置回调函数
obs_get_bucket_public_access_block_handler handler;
memset(&handler, 0, sizeof(obs_get_bucket_public_access_block_handler));
handler.response_handler.properties_callback = &response_properties_callback;
handler.response_handler.complete_callback = &response_complete_callback;
handler.get_bucket_public_access_block_callback = &get_bpa_callback;
// 初始化自定义回调数据
BpaResult result;
memset(&result, 0, sizeof(BpaResult));
result.ret_status = OBS_STATUS_BUTT;
// 获取桶级阻止公共访问配置
get_bucket_public_access_block(&options, &handler, &result);
// 判断请求是否成功
if (OBS_STATUS_OK == result.ret_status) {
printf("get bucket public access block successfully. \n");
printf("block_public_acls: %s\n", result.bpa_config.block_public_acls ? "true" : "false");
printf("ignore_public_acls: %s\n", result.bpa_config.ignore_public_acls ? "true" : "false");
printf("block_public_policy: %s\n", result.bpa_config.block_public_policy ? "true" : "false");
printf("restrict_public_buckets: %s\n", result.bpa_config.restrict_public_buckets ? "true" : "false");
}
else
{
printf("get bucket public access block failed(%s).\n", obs_get_status_name(result.ret_status));
}
// 释放分配的全局资源
obs_deinitialize();
}
// 获取桶级阻止公共访问配置的数据回调函数
obs_status get_bpa_callback(const obs_bucket_public_access_block *public_access_block, void *callback_data)
{
BpaResult *result = (BpaResult *)callback_data;
if (result && public_access_block) {
result->bpa_config = *public_access_block;
}
return OBS_STATUS_OK;
}
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
if (properties == NULL)
{
printf("error! obs_response_properties is null!");
return OBS_STATUS_OK;
}
// 打印响应信息
#define print_nonnull(name, field) \
do { \
if (properties-> field) { \
printf("%s: %s\n", name, properties->field); \
} \
} while (0)
print_nonnull("request_id", request_id);
print_nonnull("request_id2", request_id2);
print_nonnull("content_type", content_type);
if (properties->content_length) {
printf("content_length: %llu\n", properties->content_length);
}
print_nonnull("server", server);
print_nonnull("ETag", etag);
print_nonnull("expiration", expiration);
print_nonnull("website_redirect_location", website_redirect_location);
print_nonnull("version_id", version_id);
print_nonnull("allow_origin", allow_origin);
print_nonnull("allow_headers", allow_headers);
print_nonnull("max_age", max_age);
print_nonnull("allow_methods", allow_methods);
print_nonnull("expose_headers", expose_headers);
print_nonnull("storage_class", storage_class);
print_nonnull("server_side_encryption", server_side_encryption);
print_nonnull("kms_key_id", kms_key_id);
print_nonnull("customer_algorithm", customer_algorithm);
print_nonnull("customer_key_md5", customer_key_md5);
print_nonnull("bucket_location", bucket_location);
print_nonnull("obs_version", obs_version);
print_nonnull("restore", restore);
print_nonnull("obs_object_type", obs_object_type);
print_nonnull("obs_next_append_position", obs_next_append_position);
print_nonnull("obs_head_epid", obs_head_epid);
print_nonnull("reserved_indicator", reserved_indicator);
int i;
for (i = 0; i < properties->meta_data_count; i++) {
printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
properties->meta_data[i].value);
}
return OBS_STATUS_OK;
}
// 结束回调函数,可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
if (callback_data) {
BpaResult *result = (BpaResult *)callback_data;
result->ret_status = status;
} else {
printf("Callback_data is NULL");
}
if (error && error->message) {
printf("Error Message: \n %s\n", error->message);
}
if (error && error->resource) {
printf("Error Resource: \n %s\n", error->resource);
}
if (error && error->further_details) {
printf("Error further_details: \n %s\n", error->further_details);
}
if (error && error->extra_details_count) {
int i;
for (i = 0; i < error->extra_details_count; i++) {
printf("Error Extra Detail(%d):\n %s:%s\n", i, error->extra_details[i].name,
error->extra_details[i].value);
}
}
if (error && error->error_headers_count) {
int i;
for (i = 0; i < error->error_headers_count; i++) {
const char *errorHeader = error->error_headers[i];
printf("Error Headers(%d):\n %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
}
}
}