Updated on 2026-11-17 GMT+08:00

Obtaining the BPA Configuration of a Bucket (SDK for C)

If you have any questions during development, post them on the Issues page of GitHub.

Function

This API is used to obtain the BPA configuration of a bucket.

Restrictions

Method

void get_bucket_public_access_block(const obs_options *options,
    obs_get_bucket_public_access_block_handler *handler, void *callback_data);

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

options

const obs_options*

Yes

Explanation:

Context of the requested bucket. You can set the AK, SK, endpoint, bucket, timeout interval, and temporary credentials through obs_options.

Restrictions:

None

Value range:

None

Default value:

None

handler

Table 2*

Yes

Explanation:

Callback structure, including the response callback, end callback, and callback for obtaining the BPA configuration.

Restrictions:

None

Value range:

None

Default value:

None

callback_data

void *

No

Explanation:

Custom callback data.

Restrictions:

None

Value range:

None

Default value:

None

Table 2 obs_get_bucket_public_access_block_handler

Parameter

Type

Mandatory (Yes/No)

Description

response_handler

obs_response_handler

Yes

Explanation:

Response callback structure, including properties_callback and complete_callback.

Restrictions:

None

Value range:

None

Default value:

None

get_bpa_callback

Table 3*

Yes

Explanation:

Pointer to the callback function for obtaining the BPA configuration. You can read the content of obs_bucket_public_access_block in the callback.

Restrictions:

None

Value range:

None

Default value:

None

Table 3 obs_get_bucket_public_access_block_callback

Parameter

Type

Mandatory (Yes/No)

Description

public_access_block

const Table 4*

Yes

Explanation:

Bucket-level BPA configuration structure, including the statuses of four protection switches.

Restrictions:

The structure and the memory to which the pointer points will be released by the SDK after the callback. Copy them if you need to keep the data.

Value range:

None

Default value:

None

callback_data

void *

Yes

Explanation:

Pointer to the custom callback data.

Restrictions:

None

Value range:

None

Default value:

None

Table 4 obs_bucket_public_access_block

Parameter

Type

Description

block_public_acls

bool

Explanation:

Whether to block public ACLs. If this parameter is set to true, object upload and ACL modification APIs are not allowed to set public ACLs.

Restrictions:

None

Value range:

  • true: Public ACLs are blocked.
  • false: Public ACLs are not blocked.

Default value:

false

ignore_public_acls

bool

Explanation:

Whether to ignore public ACLs. If this parameter is set to true, public ACLs do not take effect when OBS checks permissions.

Restrictions:

None

Value range:

  • true: Public ACLs are ignored.
  • false: Public ACLs are not ignored.

Default value:

false

block_public_policy

bool

Explanation:

Whether to block public policies. If this parameter is set to true, public policies cannot be set using the API for modifying bucket policies.

Restrictions:

None

Value range:

  • true: Public policies are blocked.
  • false: Public policies are not blocked.

Default value:

false

restrict_public_buckets

bool

Explanation:

Whether to restrict account access. If this parameter is set to true and the bucket policy is public, only cloud service accounts and this account can access the bucket.

Restrictions:

None

Value range:

  • true: The account access is restricted.
  • false: The account access is not restricted.

Default value:

false

Sample Code

This example obtains the BPA configuration of a bucket.
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// The response callback function. In this callback, you can record the content of properties into callback_data (custom callback data).
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// The completion callback function. In this callback, you can record the content of obs_status and obs_error_details into callback_data (custom callback data).
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
// Data callback function for obtaining the BPA configuration of a bucket
obs_status get_bpa_callback(const obs_bucket_public_access_block *public_access_block, void *callback_data);
// Custom callback data. It is used to transfer the result status and configuration content between callbacks.
typedef struct BpaResult
{
    obs_bucket_public_access_block bpa_config;
    obs_status ret_status;
} BpaResult;
int main()
{
    // The following example shows how to obtain the BPA configuration of a bucket:
    // Call the obs_initialize method at the program entry to initialize global resources such as the network and memory.
    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;
    // Create and initialize options, including the access domain name (host_name), access keys (access_key_id and access_key_secret), bucket name (bucket_name), and bucket storage class (storage_class).
    init_obs_options(&options);
    
   
    // Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
   
   
    
   
    // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
    // In this example, the AK and SK are stored in environment variables for identity authentication. Before running the code in this example, configure local environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // Specify the bucket name, for example, example-bucket-name.
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    // Set the callback function.
    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;
    // Initialize the custom callback data.
    BpaResult result;
    memset(&result, 0, sizeof(BpaResult));
    result.ret_status = OBS_STATUS_BUTT;
    // Obtain the BPA configuration of the bucket.
    get_bucket_public_access_block(&options, &handler, &result);
    // Check whether the request is successful.
    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));
    }
    // Release the allocated global resources.
    obs_deinitialize();
}
// Data callback function for obtaining the BPA configuration of a bucket
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;
}
// The response callback function. In this callback, you can record the content of properties into callback_data (custom 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;
    }
// Print the response.
#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;
}
// The completion callback function. In this callback, you can record the content of obs_status and obs_error_details into callback_data (custom 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);
        }
    }
}