All Products
Search
Document Center

Object Storage Service:List buckets (C# SDK V2)

Last Updated:Jun 08, 2026

Lists all Object Storage Service (OSS) buckets owned by the current account across all regions, with optional filtering by resource group.

Before you begin

Before you begin, ensure that you have:

  • The oss:ListBuckets permission granted to the identity making the request. For details, see Grant custom permissions to a RAM user.

  • The examples use the China (Hangzhou) region (cn-hangzhou) and the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For the full list of regions and endpoints, see Regions and endpoints.

  • To filter results by resource group, pass a resource group ID in the request. The behavior depends on how the parameter is set:

    • If the resource group ID is omitted, the request does not include this parameter, and the XML response does not contain resource group information.

    • If a resource group ID is specified, OSS returns only the buckets that belong to that resource group.

    • If the resource group ID parameter is not included in the request, OSS returns all buckets owned by the requester.

List all buckets across all regions

The following example uses EnvironmentVariableCredentialsProvider to load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET) and a paginator to iterate through all results automatically.

using OSS = AlibabaCloud.OSS.V2; // Alias the OSS SDK namespace for brevity.

var region = "cn-hangzhou"; // Required. The region where your buckets are located.
var endpoint = null as string;  // Optional. Override the default endpoint (e.g., https://oss-cn-hangzhou.aliyuncs.com).

// Load default SDK configuration, then set credentials from environment variables.
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

// Create the OSS client.
using var client = new OSS.Client(cfg);

// Create a paginator to iterate through all buckets across pages.
var paginator = client.ListBucketsPaginator(new OSS.Models.ListBucketsRequest());

Console.WriteLine("Buckets:");
await foreach (var page in paginator.IterPageAsync())
{
    foreach (var bucket in page.Buckets ?? [])
    {
        // Output: bucket name, storage class, and location.
        Console.WriteLine($"Bucket: {bucket.Name}, {bucket.StorageClass}, {bucket.Location}");
    }
}

References

For the complete sample code, see list_buckets.cs.