Use listBuckets to retrieve all buckets in your Alibaba Cloud account across all regions. Results are returned in alphabetical order.
By default, listBuckets returns up to 100 buckets per call. The maximum value for max-keys is 1,000. If your account has more buckets than the configured limit, use marker-based pagination to retrieve the full list. See List all buckets with pagination.
Prerequisites
Before you begin, ensure that you have:
The
ali-osspackage installed (npm install ali-oss)OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset as environment variables
List all buckets
The following example lists all buckets in all regions under the current Alibaba Cloud account.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region where your bucket is located, for example, oss-cn-hangzhou.
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets();
console.log(result);
} catch (err) {
console.error('Failed to list buckets:', err.message);
throw err;
}
}
listBuckets();List all buckets with pagination
If your account has more than 100 buckets (or more than the value set by max-keys), use the marker parameter to paginate through results. Set marker to the name of the last bucket returned in the previous call to retrieve the next page.
const OSS = require('ali-oss');
const client = new OSS({
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
// Set max-keys to retrieve up to 1000 buckets per call.
const result = await client.listBuckets({
'max-keys': 1000,
marker: 'examplebucket',
});
console.log(result);
} catch (err) {
console.error('Failed to list buckets:', err.message);
throw err;
}
}
listBuckets();List buckets by prefix
Pass a prefix value to return only buckets whose names start with that string.
const OSS = require('ali-oss');
const client = new OSS({
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
prefix: 'example',
});
console.log(result);
} catch (err) {
console.error('Failed to list buckets:', err.message);
throw err;
}
}
listBuckets();List buckets after a marker
Set marker to a bucket name to retrieve only buckets that come after it alphabetically. Use this to resume from a known position in a large list.
const OSS = require('ali-oss');
const client = new OSS({
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
marker: 'examplebucket',
});
console.log(result);
} catch (err) {
console.error('Failed to list buckets:', err.message);
throw err;
}
}
listBuckets();Limit the number of buckets returned
Set max-keys to control how many buckets are returned per call. The value must not exceed 1,000.
const OSS = require('ali-oss');
const client = new OSS({
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function listBuckets() {
try {
const result = await client.listBuckets({
'max-keys': 500,
});
console.log(result);
} catch (err) {
console.error('Failed to list buckets:', err.message);
throw err;
}
}
listBuckets();Parameters
All parameters are optional. Omit them to list all buckets with default settings.
| Parameter | Type | Description | Constraint |
|---|---|---|---|
prefix | string | Return only buckets whose names start with this value | — |
marker | string | Return only buckets that come after this bucket name alphabetically | — |
max-keys | number | Maximum number of buckets to return per call | Default: 100; max: 1,000 |
References
Complete sample code: GitHub — ali-sdk/ali-oss
API reference: ListBuckets (GetService)