All Products
Search
Document Center

Object Storage Service:List buckets (Java SDK)

Last Updated:Jul 31, 2025

A bucket is a container for storing objects. All objects are stored in buckets. You can list buckets that meet specified conditions across all regions in your Alibaba Cloud account. Buckets are listed in alphabetical order.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.

  • To list buckets, you must have the oss:ListBuckets permission. For more information, see Attach a custom policy to a RAM user.

  • If you have Infrequent Access (IA) or Archive buckets, use OSS SDK for Java 2.6.0 or later.

  • The following code lists buckets across all regions in the current Alibaba Cloud account. You cannot use this code to list buckets in a specific region. The list result is independent of the region associated with the specified endpoint.

List all buckets

The following code provides an example of how to list all buckets across all regions in the current Alibaba Cloud account.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.Bucket; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Set the region where the bucket is located. In this example, the region is set to cn-hangzhou, which indicates the China (Hangzhou) region. String region = "cn-hangzhou"; // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // List all buckets in all regions within the current Alibaba Cloud account. List<Bucket> buckets = ossClient.listBuckets(); for (Bucket bucket : buckets) { System.out.println(" - " + bucket.getName()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }

List buckets in a resource group

The following code provides an example of how to list buckets in a specified resource group in the current Alibaba Cloud account.

Note

An Alibaba Cloud account contains one default resource group and multiple custom resource groups. If you do not specify a resource group ID when you create a bucket, the bucket is added to the default resource group.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the ID of the resource group. If you do not specify a resource group ID, the buckets in the default resource group are listed. String rsId = "rg-aek27tc****"; // Set the region where the bucket is located. In this example, the region is set to cn-hangzhou, which indicates the China (Hangzhou) region. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // List buckets. ListBucketsRequest listBucketsRequest = new ListBucketsRequest(); // List buckets in the specified resource group within the current Alibaba Cloud account. listBucketsRequest.setResourceGroupId(rsId); BucketList bucketList = ossClient.listBuckets(listBucketsRequest); for (Bucket bucket : bucketList.getBucketList()) { System.out.println(" - " + bucket.getName()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } } 

List buckets whose names contain a specified prefix

The following code provides an example of how to list buckets whose names start with the prefix `example` across all regions in the current Alibaba Cloud account.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.Bucket; import com.aliyun.oss.model.BucketList; import com.aliyun.oss.model.ListBucketsRequest; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Set the region where the bucket is located. In this example, the region is set to cn-hangzhou, which indicates the China (Hangzhou) region. String region = "cn-hangzhou"; // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { ListBucketsRequest listBucketsRequest = new ListBucketsRequest(); // List buckets whose names contain the specified prefix in all regions within the current Alibaba Cloud account. listBucketsRequest.setPrefix("example"); BucketList bucketList = ossClient.listBuckets(listBucketsRequest); for (Bucket bucket : bucketList.getBucketList()) { System.out.println(" - " + bucket.getName()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }

List buckets after a specified marker

The following code provides an example of how to list buckets whose names are alphabetically after `examplebucket` across all regions in the current Alibaba Cloud account.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; import java.util.List; public class Demo { public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException { // In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name. Example: examplebucket. String bucketName = "examplebucket"; // Set the maximum number of buckets to return on each page to 200. int maxKeys = 200; // Set the region where the bucket is located. In this example, the region is set to cn-hangzhou, which indicates the China (Hangzhou) region. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // List buckets that are alphabetically after the specified marker in all regions within the current Alibaba Cloud account. String nextMarker = bucketName; BucketList bucketListing; do { bucketListing = ossClient.listBuckets(new ListBucketsRequest().withMarker(nextMarker).withMaxKeys(maxKeys)); List<Bucket> sums = bucketListing.getBucketList(); for (Bucket s : sums) { System.out.println("\t" + s.getName()); } nextMarker = bucketListing.getNextMarker(); } while (bucketListing.isTruncated()); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }

List a specified number of buckets

The following code provides an example of how to list buckets across all regions in the current Alibaba Cloud account and limit the number of returned buckets to 500.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.Bucket; import com.aliyun.oss.model.BucketList; import com.aliyun.oss.model.ListBucketsRequest; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Set the region where the bucket is located. In this example, the region is set to cn-hangzhou, which indicates the China (Hangzhou) region. String region = "cn-hangzhou"; // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Create an OSSClient instance. // When the OSSClient instance is no longer used, call the shutdown method to release the resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { ListBucketsRequest listBucketsRequest = new ListBucketsRequest(); // List buckets in all regions within the current Alibaba Cloud account and set the maximum number of buckets to return in the response to 500. The default value is 100. The maximum value is 1,000. listBucketsRequest.setMaxKeys(500); BucketList bucketList = ossClient.listBuckets(listBucketsRequest); for (Bucket bucket : bucketList.getBucketList()) { System.out.println(" - " + bucket.getName()); } } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }

References