This topic describes how to list buckets that belong to the current account and meet specified conditions across all regions.
Notes
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouas an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services within the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.To list buckets, you must have the
oss:ListBucketspermission. For more information, see Grant custom permissions to a RAM user.When you use an SDK to list buckets, you can specify a resource group ID to list only the buckets that belong to that resource group.
By default, if you do not specify a resource group ID, the request does not include this parameter, and the XML response does not contain information about resource groups.
If the resource group ID parameter is included in the request, OSS returns all buckets that belong to the specified resource group.
If the resource group ID parameter is not included in the request, OSS returns all buckets that belong to the requester.
List all buckets in all regions for the current account
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. In this example, the China (Hangzhou) region is used. Set the region to cn-hangzhou.
var endpoint = null as string; // Optional. Specify the domain name used to access the OSS service. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);
// Create a paginator for the ListBuckets operation to process paged results.
// ListBucketsRequest is a request model defined by the SDK. In this example, the default constructor is used to obtain all buckets.
var paginator = client.ListBucketsPaginator(new OSS.Models.ListBucketsRequest());
Console.WriteLine("Buckets:");
await foreach (var page in paginator.IterPageAsync())
{
// Traverse each bucket in the current page.
foreach (var bucket in page.Buckets ?? [])
{
// Print bucket information: name, storage class, and location.
Console.WriteLine($"Bucket:{bucket.Name}, {bucket.StorageClass}, {bucket.Location}");
}
}References
For the complete sample code to list buckets, see list_buckets.cs.