Buckets are listed in alphabetical order in Object Storage Service (OSS). This topic describes how to list all buckets that belong to the current Alibaba Cloud account.

Sample code

The following code provides an example on how to list all buckets that belong to the current Alibaba Cloud account:

using Aliyun.OSS;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";

// Initialize an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);

// List all buckets that belong to the current Alibaba Cloud account. 
public void ListBuckets()
{
    try
    {
        var buckets = client.ListBuckets();

        Console.WriteLine("List bucket succeeded");
        foreach (var bucket in buckets)
        {
            Console.WriteLine("Bucket name:{0}, Location:{1}, Owner:{2}", bucket.Name, bucket.Location, bucket.Owner);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("List bucket failed. {0}", ex.Message);
    }
}    

References

  • For more information about the complete sample code that is used to list buckets, visit GitHub.
  • For the API operation that you can call to list buckets, see GetService (ListBuckets).