Updated on 2024-04-29 GMT+08:00

Listing Buckets

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

You can call list_bucket_obs to list buckets.

Parameter Description

Field

Type

Mandatory or Optional

Description

option

The context of the bucket. For details, see Configuring option.

Mandatory

Bucket parameter

handler

obs_list_service_obs_handler *

Mandatory

Callback function

callback_data

void *

Optional

Callback data

Sample Code

static void test_list_bucket_obs()
{ 
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");

    list_service_data data;
    memset_s(&data, sizeof(list_service_data), 0, sizeof(list_service_data));
    // Customize response callback function.
    obs_list_service_obs_handler listHandler =
    { 
        {NULL,
        &list_bucket_complete_callback },
        &listServiceObsCallback
    };
    // List buckets.
    list_bucket_obs(&option,&listHandler,&data);
    if (data.ret_status == OBS_STATUS_OK) 
    {
        printf("list bucket successfully. \n");
    }
    else
    {
        printf("list bucket failed(%s).\n", obs_get_status_name(data.ret_status));
    }
}

Obtained bucket names are listed in the lexicographical order.