Obtaining the Public Access Status 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 public access status of a bucket. The public access status of a bucket is determined based on multiple factors, including the bucket policy, ACL, and BPA configuration. It is used to determine whether the bucket is at risk of public access.
Restrictions
- To obtain the public access status of a bucket, you must be the bucket owner or have the required permission (obs:bucket:GetBucketPublicStatus granted using IAM or GetBucketPublicStatus 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_status(const obs_options *options,
obs_get_bucket_public_status_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 public access status of a bucket. 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 function structure. Restrictions: None Value range: None Default value: None | |
| get_public_status_callback | Yes | Explanation: Pointer to the callback function for obtaining the public access status of a bucket. You can read the content of obs_bucket_public_status in the callback. Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| policy_status | const Table 4* | Yes | Explanation: Bucket public access status structure, including information about whether the bucket is public. 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 |
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 PublicStatusResult
{
bool is_public;
obs_status ret_status;
} PublicStatusResult;
obs_status get_public_status_callback(const obs_bucket_public_status *public_status, void *callback_data)
{
PublicStatusResult *result = (PublicStatusResult *)callback_data;
if (public_status) {
result->is_public = public_status->is_public;
}
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_status_handler handler = {0};
handler.response_handler.properties_callback = &response_properties_callback;
handler.response_handler.complete_callback = &response_complete_callback;
handler.get_public_status_callback = &get_public_status_callback;
PublicStatusResult result;
memset(&result, 0, sizeof(PublicStatusResult));
result.ret_status = OBS_STATUS_BUTT;
get_bucket_public_status(&options, &handler, &result);
if (OBS_STATUS_OK == result.ret_status) {
printf("get bucket public status successfully.\n");
printf("is_public: %s\n", result.is_public ? "true" : "false");
} else {
printf("get bucket public status 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) {
PublicStatusResult *result = (PublicStatusResult *)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