All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Mar 01, 2024

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are contained in buckets. Buckets are listed in alphabetical order. You can list buckets that belong to the current Alibaba Cloud account in all regions and meet specific conditions.

Usage notes

Before you run the sample code provided in this topic, you must initialize an OSSClient instance. The following sample code provides an example on how to initialize an OSSClient instance:

// Specify the URL of the Security Token Service (STS) application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider. The token is automatically updated after it expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);

List all buckets within an Alibaba Cloud account

The following sample code provides an example on how to list buckets in all regions within the current Alibaba Cloud account:

// Specify the URL of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider. The token is automatically updated after it expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);

// List all buckets that belong to the current Alibaba Cloud account in all regions. 
ListBucketsRequest request = new ListBucketsRequest();
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
    @Override
    public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
        List<OSSBucketSummary> buckets = result.getBuckets();
        for (int i = 0; i < buckets.size(); i++) {
            Log.i("info", "name: " + buckets.get(i).name + " "
                    + "location: " + buckets.get(i).location);
        }
    }

    @Override
    public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions, such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

List buckets whose names contain a specific prefix

The following sample code provides an example on how to list buckets whose names contain the example prefix in all regions within the current Alibaba Cloud account:

Note

Prefix-based listing uses fuzzy matching and returns buckets whose names start with the specified prefix. For example, if the specified prefix is a, the listing operation returns all buckets whose names start with "a" within the account, such as abucket and abcbucket.

// Specify the URL of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider. The token is automatically updated after it expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);

ListBucketsRequest request = new ListBucketsRequest();
// List all buckets whose names start with the example prefix within the current account. 
request.setPrefix("example");
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
    @Override
    public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
        List<OSSBucketSummary> buckets = result.getBuckets();
        for (int i = 0; i < buckets.size(); i++) {
            Log.i("i", "name: " + buckets.get(i).name + " "
                    + "location: " + buckets.get(i).location);
        }
    }

    @Override
    public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions, such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

List buckets whose names are alphabetically after the bucket specified by marker

The following sample code provides an example on how to list buckets whose names are alphabetically after the bucket named examplebucket in all regions within the current Alibaba Cloud account:

Note

Prefix-based listing uses fuzzy matching and returns buckets whose names start with the specified prefix. For example, if the specified prefix is a, the listing operation returns all buckets whose names start with "a" within the account, such as abucket and abcbucket.

// Specify the URL of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider. The token is automatically updated after it expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);

ListBucketsRequest request = new ListBucketsRequest();
// List all buckets whose names are alphabetically after examplebucket within the current account. 
request.setMarker("examplebucket");
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
    @Override
    public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
        List<OSSBucketSummary> buckets = result.getBuckets();
        for (int i = 0; i < buckets.size(); i++) {
            Log.i("i", "name: " + buckets.get(i).name + " "
                    + "location: " + buckets.get(i).location);
        }
    }

    @Override
    public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions, such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

List a specific number of buckets

The following sample code provides an example on how to list the buckets in all regions within the current Alibaba Cloud account and set the maximum number of buckets that can be listed to 500:

// Specify the URL of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider. The token is automatically updated after it expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);

ListBucketsRequest request = new ListBucketsRequest();
// List all buckets that belong to the current account. Set the maximum number of buckets that can be returned by the listing operation to 500. The default value is 100, and the maximum value is 1000. 
request.setMaxKeys(500);
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
    @Override
    public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
        List<OSSBucketSummary> buckets = result.getBuckets();
        for (int i = 0; i < buckets.size(); i++) {
            Log.i("i", "name: " + buckets.get(i).name + " "
                    + "location: " + buckets.get(i).location);
        }
    }

    @Override
    public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions, such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • For the complete sample code that is used to list buckets, visit GitHub.

  • For more information about the API operation that you can call to list buckets, see ListBuckets (GetService).