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.
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:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
// List all buckets in all regions within the current Alibaba Cloud account.
const result = await client.listBuckets();
console.log(result);
} catch (err) {
console.log(err);
}
}
listBuckets();
List buckets whose names contain a specified 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:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
// Specify the prefix that is contained in the names of the buckets that you want to list.
prefix: 'example'
});
// List the buckets whose names contain the specified prefix in all regions within the current Alibaba Cloud account.
console.log(result);
} catch (err) {
console.log(err);
}
}
listBuckets();
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:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
// Specify the value of marker.
marker: 'examplebucket'
});
// List the buckets whose names are alphabetically after examplebucket in all regions within the current Alibaba Cloud account.
console.log(result);
} catch (err) {
console.log(err);
}
}
listBuckets();
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:
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
// Specify the max-keys parameter. This parameter is used to specify the maximum number of buckets to be listed. The value of max-keys cannot exceed 1000. By default, if max-keys is not specified, up to 100 buckets are returned.
// List 500 buckets.
'max-keys': 500
});
console.log(result);
} catch (err) {
console.log(err);
}
}
listBuckets();
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).