All Products
Search
Document Center

Object Storage Service:List buckets (C SDK)

Last Updated:Mar 20, 2026

Use the C SDK to list all buckets in your Alibaba Cloud account across all regions. Buckets are returned in alphabetical order.

Prerequisites

Before you begin, ensure that you have:

  • The oss:ListBuckets permission. For more information, see Attach a custom policy to a RAM user

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with your AccessKey ID and AccessKey secret

Usage notes

  • This example uses the public endpoint for the China (Hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For details, see Regions and endpoints.

  • This example creates an OSSClient instance using an OSS endpoint. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization.

Sample code

The following example lists all buckets in the current Alibaba Cloud account.

#include "oss_api.h"
#include "aos_http_io.h"

/* Set yourEndpoint to the endpoint for your region.
   Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou). */
const char *endpoint = "yourEndpoint";

/* Set yourRegion to the region ID for your bucket.
   Example: cn-hangzhou for China (Hangzhou). */
const char *region = "yourRegion";

void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);

    /* Initialize the aos_string_t type with a char* string. */
    aos_str_set(&options->config->endpoint, endpoint);

    /* Load credentials from environment variables.
       Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example. */
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));

    /* Required for Signature Version 4. */
    aos_str_set(&options->config->region, region);
    options->config->signature_version = 4;

    /* Set is_cname to 0 to use an OSS endpoint instead of a CNAME. */
    options->config->is_cname = 0;

    /* Set network parameters such as the timeout period. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}

int main(int argc, char *argv[])
{
    /* Initialize global resources (network, memory) at program entry. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }

    /* Create a memory pool. Pass NULL to create a top-level pool. */
    aos_pool_t *pool;
    aos_pool_create(&pool, NULL);

    /* Allocate and initialize the client options. */
    oss_request_options_t *oss_client_options;
    oss_client_options = oss_request_options_create(pool);
    init_options(oss_client_options);

    /* Initialize request parameters. */
    aos_table_t *resp_headers = NULL;
    aos_status_t *resp_status = NULL;
    oss_list_buckets_params_t *params = NULL;
    oss_list_bucket_content_t *content = NULL;
    int size = 0;

    params = oss_create_list_buckets_params(pool);

    /* List all buckets in the account. */
    resp_status = oss_list_bucket(oss_client_options, params, &resp_headers);
    if (aos_status_is_ok(resp_status)) {
        printf("list buckets succeeded\n");
    } else {
        printf("list buckets failed\n");
    }

    /* Print each bucket name. */
    aos_list_for_each_entry(oss_list_bucket_content_t, content, &params->bucket_list, node) {
        printf("BucketName: %s\n", content->name.data);
        ++size;
    }

    /* Release the memory pool and free global resources. */
    aos_pool_destroy(pool);
    aos_http_io_deinitialize();
    return 0;
}

References

For information about the underlying API operation, see ListBuckets (GetService).