Tous les produits
Search
Centre de documentation

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

Dernière mise à jour :Sep 24, 2026

Un bucket est un conteneur destiné aux objets stockés dans Object Storage Service (OSS). Tous les objets d'OSS sont contenus dans des buckets. Les buckets sont répertoriés par ordre alphabétique. Vous pouvez répertorier les buckets appartenant à votre compte Alibaba Cloud dans toutes les régions et répondant à des conditions spécifiques.

Répertorier tous les buckets d’un compte Alibaba Cloud

L'exemple de code suivant montre comment répertorier les buckets dans toutes les régions de votre compte 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();

Répertorier les buckets dont le nom contient un préfixe spécifié

L'exemple de code suivant montre comment répertorier les buckets dont le nom contient le préfixe « example » dans toutes les régions de votre compte 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();

Répertorier les buckets dont le nom suit alphabétiquement celui indiqué par marker

L'exemple de code suivant montre comment répertorier les buckets dont le nom suit alphabétiquement le bucket nommé « examplebucket » dans toutes les régions de votre compte 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();

Répertorier un nombre spécifique de buckets

L'exemple de code suivant montre comment répertorier les buckets dans toutes les régions de votre compte Alibaba Cloud et définir le nombre maximal de buckets à répertorier à 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();

Références

  • Pour consulter l'exemple de code complet utilisé pour répertorier les buckets, rendez-vous sur GitHub.

  • Pour plus d'informations sur l'opération API que vous pouvez appeler pour répertorier les buckets, consultez ListBuckets (GetService).