List buckets (C++ SDK V2)

更新时间:
复制 MD 格式

List all buckets in your Alibaba Cloud account by using OSS C++ SDK V2.

Usage notes

  • Before you run the sample code, replace <region> with an actual region, such as cn-hangzhou.

  • The sample code uses environment variables for access credentials. For more information, see Configure access credentials.

  • To list buckets, you must have the oss:ListBuckets permission.

Sample code

List buckets in a single request

Call listBuckets to list buckets. A single call returns up to 100 buckets. To list all buckets, use the paginator described below.

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto outcome = client.listBuckets(oss::models::ListBucketsRequest());

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to list buckets"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Successfully listed buckets"
              << ", status: " << result.getStatusCode()
              << ", requestId: " << result.getRequestId() << std::endl;

    for (const auto& b : result.getBuckets()) {
        std::cout << "Bucket: " << b.name
                  << ", Location: " << b.location
                  << ", StorageClass: " << b.storageClass
                  << ", CreationDate: " << b.creationDate << std::endl;
    }
    return 0;
}
          

List all buckets using a paginator

A paginator automatically fetches subsequent pages and iterates through all buckets without manually handling markers or tokens.

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/Paginator.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto paginator = oss::makePaginator(client, oss::models::ListBucketsRequest());

    int pageNum = 0;
    int totalBuckets = 0;
    while (paginator.hasNext()) {
        auto outcome = paginator.nextPage();
        if (!outcome.has_value()) {
            auto& e = outcome.error();
            std::cerr << "Failed to list buckets"
                      << ", code: " << e.getCode()
                      << ", message: " << e.getMessage()
                      << ", requestId: " << e.getRequestId() << std::endl;
            return 1;
        }
        auto& result = outcome.value();
        pageNum++;
        std::cout << "Page " << pageNum << ", isTruncated: " << result.getIsTruncated() << std::endl;
        for (const auto& b : result.getBuckets()) {
            std::cout << "  Bucket: " << b.name
                      << ", Location: " << b.location
                      << ", StorageClass: " << b.storageClass << std::endl;
            totalBuckets++;
        }
    }
    std::cout << "Total " << totalBuckets << " buckets in " << pageNum << " pages" << std::endl;
    return 0;
}
          

References