A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to list buckets.

Buckets are listed in alphabetical order. You can list all buckets or only buckets that meet specified conditions. To list buckets of the Infrequent Access (IA) or Archive storage class, use OSS SDK for Java 2.6.0 or later.

For more information about how to list buckets, see GetBucket (ListObject).

List all buckets

The following code provides an example on how to list all buckets:

// This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// List all buckets.
List<Bucket> buckets = ossClient.listBuckets();
for (Bucket bucket : buckets) {
    System.out.println(" - " + bucket.getName());
}

// Shut down the OSSClient instance.
ossClient.shutdown();                    

List buckets whose names contain a specified prefix

The following code provides an example on how to list buckets whose names contain a specified prefix:

ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
// List the buckets whose names contain a specified prefix.
listBucketsRequest.setPrefix("<yourBucketPrefix>");
BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
for (Bucket bucket : bucketList.getBucketList()) {
    System.out.println(" - " + bucket.getName());
}
			

List buckets that follow a specified marker

The marker parameter specifies the name of the bucket after which the list begins. The following code provides an example on how to list buckets that follow a specified marker:

ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
// List the buckets that follow a specified marker.
listBucketsRequest.setMarker("<yourBucketMarker>");
BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
for (Bucket bucket : bucketList.getBucketList()) {
    System.out.println(" - " + bucket.getName());
}                    

List a specified number of buckets

The following code provides an example on how to list a specified number (MaxKeys) of buckets:

ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
// Set the number of buckets to list each time to 500. The default value is 100. The maximum value is 1000.
listBucketsRequest.setMaxKeys(500);
BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
for (Bucket bucket : bucketList.getBucketList()) {
    System.out.println(" - " + bucket.getName());
}