A bucket is a container that stores objects. You can list all buckets or only buckets that meet specified conditions.

List all buckets

The following code provides an example on how to list all buckets:

const OSS = require('ali-oss');

const client = new OSS({
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function listBuckets() {
  try {
    const result = await client.listBuckets();
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBuckets();

List buckets whose names contain a specified prefix

The following code provides an example on how to list buckets whose names contain a specified prefix:

const OSS = require('ali-oss');

const client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function listBuckets() {
  try {
    const result = await client.listBuckets({
      prefix: 'prefix' // Specify the prefix that is contained in the buckets to list.
    });
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBuckets();

List buckets that follow a specified marker

The marker parameter specifies the name of the bucket after which the list begins. The following code provides an example on how to list buckets that follow a specified marker:

const OSS = require('ali-oss');

const client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function listBuckets() {
  try {
    const result = await client.listBuckets({
      marker: 'marker' // List buckets that follow a specified marker.
    });
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBuckets();

List a specified number of buckets

The following code provides an example on how to list a specified number (MaxKeys) of buckets:

const OSS = require('ali-oss');

const client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function listBuckets() {
  try {
    const result = await client.listBuckets({
      'max-keys': 20 // Set the number of buckets to list each time to 20.
    });
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBuckets();