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

List all buckets that belong to an Alibaba Cloud account

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

// Specify the address of the Security Token Service (STS) application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to ensure that the token is automatically updated when the token expires. 
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, null);

// List all buckets that belong to the current account. 
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 in all regions within the current 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 ensure 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();
// List all buckets whose names contain the example prefix under 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++) {
            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 marker

The following code provides an example on how to list buckets whose names are alphabetically after examplebucket in all regions within the current 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 ensure 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();
// List all buckets whose names are alphabetically after examplebucket under 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++) {
            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 buckets that belong to the current Alibaba Cloud account in all regions and set the maximum number of buckets to 500:

// Specify the address of the STS application server. Example: http://example.com. 
String stsServer = "http://example.com";
// We recommend that you use OSSAuthCredentialsProvider to ensure 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();
// List all buckets that belong to the current account. Set the maximum number of buckets that can be listed to 500. Maximum value: 1000. Default value: 100. 
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());
        }
    }
});

References

  • For more information about 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 GetService (ListBuckets).