A bucket is a container that is used to store objects in Object Storage Service (OSS). All objects in OSS are contained in buckets. Bucket names are listed in alphabetical order. You can list buckets that belong to the current Alibaba Cloud account in all regions and meet the specific conditions.

List all buckets that belong to an Alibaba Cloud account

The following code provides an example on how to list buckets that belong to the current Alibaba Cloud account in all regions:

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',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

async function listBuckets() {
  try {
    // List the buckets that belong to the current Alibaba Cloud account in all regions. 
    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 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',  
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

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 that contain the specified prefix in their names and belong to the current Alibaba Cloud account in all regions. 
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBuckets();

List buckets whose names are alphabetically after the bucket specified by marker

The following code provides an example on how to list buckets whose names are alphabetically after 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',  
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

async function listBuckets() {
  try {
    const result = await client.listBuckets({
      // Set marker to examplebucket. 
      marker: 'examplebucket' 
    });
    // List the buckets whose names are alphabetically after examplebucket within the current Alibaba Cloud account in all regions. 
    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 buckets that belong to the current Alibaba Cloud account in all regions and set the maximum number of buckets 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',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

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, 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 GetService (ListBuckets).