All Products
Search
Document Center

Object Storage Service:List buckets

Last Updated:Oct 19, 2023

A bucket is a container for objects stored 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 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
});

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 the buckets whose names contain a specified prefix

The following sample code provides an example on how to list the 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
});

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 the buckets whose names are alphabetically after the bucket specified by marker

The following sample code provides an example on how to list the 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
});

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 specified number of buckets

The following sample code provides an example on how to list a maximum of 500 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
});

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).