All Products
Search
Document Center

Object Storage Service:List buckets (C++ SDK)

Last Updated:Mar 20, 2026

OSS lists buckets in alphabetical order. This page shows how to list all buckets in your Alibaba Cloud account using the C++ SDK.

Prerequisites

Before you begin, make sure you have:

Usage notes

  • The examples on this page use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.

List all buckets

How it works

ListBucketsRequest sends a request to the ListBuckets API. On success, client.ListBuckets(request) returns an outcome object. Call outcome.result().Buckets() to get the collection of bucket objects, then iterate over it to read each bucket's name with result.Name(). The total count is available via outcome.result().Buckets().size().

Sample code

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";

    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* List all buckets under the current account. */
    ListBucketsRequest request;
    auto outcome = client.ListBuckets(request);

    if (outcome.isSuccess()) {
        /* Print bucket information. */
        std::cout << "success, bucket count: " << outcome.result().Buckets().size() << std::endl;
        for (auto result : outcome.result().Buckets())
        {
            std::cout << result.Name() << std::endl;
        }
    }
    else {
        /* Handle the exception. */
        std::cout << "ListBuckets fail"
            << ", code: " << outcome.error().Code()
            << ", message: " << outcome.error().Message()
            << ", requestId: " << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Expected output

When the call succeeds, the output looks similar to:

success, bucket count: 3
my-bucket-a
my-bucket-b
my-bucket-c

What's next