A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are contained in buckets. Use asyncListBuckets to retrieve all buckets in your Alibaba Cloud account across all regions. Buckets are listed in alphabetical order. You can filter results by name prefix, starting position (marker), or page size.
Prerequisites
Before you begin, ensure that you have:
An initialized
OSSClientinstance. For setup instructions, see Initialization.
List all buckets
The following example lists all buckets in your account across all regions.
// List all buckets in all regions of your 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++) {
Log.i("info", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});Filter buckets
Use ListBucketsRequest parameters to narrow results by prefix, starting position, or page size.
| Parameter | Method | Description | Default | Max |
|---|---|---|---|---|
prefix | setPrefix(String) | Returns only buckets whose names start with the specified prefix. For example, "a" matches abucket and abcbucket. | — | — |
marker | setMarker(String) | Returns buckets that come alphabetically after the specified bucket name. The specified bucket itself is not included in the results. | — | — |
maxKeys | setMaxKeys(int) | Maximum number of buckets to return per request. | 100 | 1,000 |
Filter by prefix
ListBucketsRequest request = new ListBucketsRequest();
// Return only buckets whose names start with "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++) {
Log.i("i", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});Filter by marker
ListBucketsRequest request = new ListBucketsRequest();
// Return buckets that come alphabetically after "examplebucket".
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) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});Limit results per request
The default page size is 100 buckets. Set maxKeys to control how many buckets are returned per call (up to 1,000).
ListBucketsRequest request = new ListBucketsRequest();
// Return up to 500 buckets per call. Default: 100. Maximum: 1,000.
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) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});References
For complete sample code, see the GitHub sample.
For the API reference for this operation, see ListBuckets (GetService).