Updated on 2026-08-03 GMT+08:00

Obtaining the Policy of a Bucket (SDK for C)

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

Function

This API obtains the policy of a bucket.

Restrictions

Method

void get_bucket_policy(const obs_options *options, int policy_return_size,
                      char *policy_return, obs_response_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

policy_return_size

int

Yes

Explanation:

Size of the buffer for storing the policy content, in bytes.

Restrictions:

The value must be greater than or equal to the actual policy content length (including the terminator). Otherwise, the policy content will be truncated. It is recommended that the buffer size be greater than or equal to 21 KB to accommodate the maximum policy content.

Value range:

A positive integer

Default value:

None

policy_return

char *

Yes

Explanation:

Buffer for storing the returned policy content.

Restrictions:

The buffer is allocated and released by the caller. Ensure that the buffer size is greater than or equal to the value specified by policy_return_size.

Value range:

None

Default value:

None

handler

obs_response_handler *

Yes

Explanation:

A callback structure where all members are pointers to callback functions, used to set the callback functions that handle response data. You can set a callback function to copy the response data from the server to callback_data (custom callback data).

Restrictions:

None

Value range:

None

Default value:

None

callback_data

void *

No

Explanation:

Custom callback data.

Restrictions:

None

Value range:

None

Default value:

None

Sample Code

This example obtains the policy of a bucket.
#include "eSDKOBS.h"
#include <stdio.h>
// Response callback function
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);
int main()
{
    // 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 and secret_access_key), and bucket name (bucket_name).
    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");
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // Specify the bucket name, for example, example-bucket-name.
    options.bucket_options.bucket_name = "example-bucket-name";
    // Set the response callback function.
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    // Allocate the buffer for the policy content. It is recommended that the buffer size be greater than or equal to 21 KB.
    #define MAX_POLICY_BUFFER_SIZE (21 * 1024)
    char policy[MAX_POLICY_BUFFER_SIZE] = {0};
    ret_status = OBS_STATUS_BUTT;
    get_bucket_policy(&options, sizeof(policy), policy, &response_handler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf("get bucket policy successfully.\npolicy:\n%s\n", policy);
    } else {
        printf("get bucket policy failed(%s).\n", obs_get_status_name(ret_status));
    }
    // Release the allocated global resources.
    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) { *(obs_status*)callback_data = status; }
    if (error && error->message) { printf("Error: %s\n", error->message); }
}