A bucket is a container that stores objects. All objects are stored in a bucket. You can list buckets that meet specific conditions across all regions in your Alibaba Cloud account. The listed buckets are sorted alphabetically.
Precautions
For more information about OSS-supported regions and their Endpoints, see Regions and Endpoints.
To list buckets, you must have the
oss:ListBucketspermission. For more information, see Grant custom access policies to RAM users.The following code lists buckets across all regions in your Alibaba Cloud account. This operation does not support listing buckets in a specific region. The result is not affected by the region of the specified endpoint.
Sample code
The following code shows how to list all buckets across all regions in your Alibaba Cloud account.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Replace with the Access Key ID of the Security Token Service (STS) temporary access credential.
accessKeyId: 'yourAccessKeyId',
// Replace with the Access Key Secret of the STS temporary access credential.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the Security Token of the STS temporary access credential.
securityToken: 'yourSecurityToken',
});
// List all buckets.
const listBuckets = async () => {
try {
// Call the listBuckets method to list all buckets.
const res = await client.listBuckets({});
// Print the result.
console.log(JSON.stringify(res));
} catch (err) {
// Catch and handle request errors.
if (err instanceof RequestError) {
console.log('Error code: ', err.code); // Error code
console.log('Error message: ', err.message); // Error description
console.log('Request ID: ', err.requestId); // Unique identifier of the request
console.log('HTTP status code: ', err.status); // HTTP response status code
console.log('Error category: ', err.ec); // Error category
} else {
console.log('Unknown error: ', err); // Error that is not of the RequestError type
}
}
};
// Call the function to list all buckets.
listBuckets();
Other scenarios
References
For more information about the API operation for listing buckets, see ListBuckets (GetService).