Todos os produtos
Search
Central de documentação

Object Storage Service:Listar buckets (OSS SDK for Node.js)

Última atualização: Jul 03, 2026

Um bucket é um contêiner para objetos armazenados no Object Storage Service (OSS). Todos os objetos do OSS residem em buckets, listados em ordem alfabética. Você pode listar os buckets da conta atual do Alibaba Cloud em todas as regiões que atendam a condições específicas.

Listar todos os buckets de uma conta do Alibaba Cloud

O código de exemplo a seguir mostra como listar buckets em todas as regiões da conta atual do Alibaba Cloud:

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 running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
  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();

Listar buckets cujos nomes contêm um prefixo especificado

O código de exemplo a seguir mostra como listar buckets cujos nomes contêm o prefixo example em todas as regiões da conta atual do Alibaba Cloud:

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 running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  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();

Listar buckets cujos nomes vêm após o bucket especificado por marker em ordem alfabética

O código de exemplo a seguir mostra como listar buckets cujos nomes aparecem depois do bucket chamado examplebucket, em ordem alfabética, em todas as regiões da conta atual do Alibaba Cloud:

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 running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  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();

Listar um número específico de buckets

O código de exemplo a seguir mostra como listar buckets em todas as regiões da conta atual do Alibaba Cloud e definir o número máximo de buckets listados como 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 running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  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();

Referências

  • Para obter o código de exemplo completo sobre como listar buckets, acesse o GitHub.

  • Para obter mais informações sobre a operação de API usada para listar buckets, consulte ListBuckets (GetService).