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
- To obtain the BPA configuration of a bucket, you must be the bucket owner or have the required permission (obs:bucket:GetBucketPublicAccessBlock granted using IAM or GetBucketPublicAccessBlock granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
Method
void get_bucket_public_access_block(const obs_options *options,
obs_get_bucket_public_access_block_handler *handler, void *callback_data); 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 | 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 |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| response_handler | Yes | Explanation: Response callback structure, including properties_callback and complete_callback. Restrictions: None Value range: None Default value: None | |
| get_bpa_callback | 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 |
| 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 |
| 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:
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:
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:
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:
Default value: false |
Sample Code
#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);
typedef struct BpaResult
{
obs_bucket_public_access_block bpa_config;
obs_status ret_status;
} BpaResult;
obs_status get_bpa_callback(const obs_bucket_public_access_block *public_access_block, void *callback_data)
{
BpaResult *result = (BpaResult *)callback_data;
if (public_access_block) {
result->bpa_config = *public_access_block;
}
return OBS_STATUS_OK;
}
int main()
{
obs_initialize(OBS_INIT_ALL);
obs_options options;
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");
options.bucket_options.bucket_name = "example-bucket-name";
obs_get_bucket_public_access_block_handler handler = {0};
handler.response_handler.properties_callback = &response_properties_callback;
handler.response_handler.complete_callback = &response_complete_callback;
handler.get_bpa_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 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) {
BpaResult *result = (BpaResult *)callback_data;
result->ret_status = status;
}
if (error && error->message) { printf("Error: %s\n", error->message); }
} Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot