A bucket is a container that is used to store objects in Object Storage Service (OSS). Every object is contained in a bucket. You can list all buckets that belong to an Alibaba Cloud account or buckets that meet the specified conditions. The buckets are listed in alphabetical order of the bucket names.

List all buckets that belong to an Alibaba Cloud account

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

// Specify the address of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to make sure that the token is automatically updated when the token expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, null);

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++) {
            OSSLog.logDebug("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 specified prefix

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

// Specify the address of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to make sure that the token is automatically updated when the token expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, null);

ListBucketsRequest request = new ListBucketsRequest();
// Specify the prefix. Example: example. 
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++) {
            OSSLog.logDebug("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 a marker

The following code provides an example on how to list buckets whose names are alphabetically after examplebucket:

// Specify the address of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to make sure that the token is automatically updated when the token expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, null);

ListBucketsRequest request = new ListBucketsRequest();
// Specify the marker. Example: examplebucket. Buckets whose names are alphabetically after the bucket specified by the marker are returned. 
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++) {
            OSSLog.logDebug("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 specified number of buckets

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

// Specify the address of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to make sure that the token is automatically updated when the token expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, null);


ListBucketsRequest request = new ListBucketsRequest();
// Set the maximum number of buckets that can be listed each time 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++) {
            OSSLog.logDebug("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());
        }
    }
});