Todos os produtos
Search
Central de documentação

Object Storage Service:Bucket tagging (Node.js SDK)

Última atualização: Sep 23, 2026

As tags de bucket permitem classificar e gerenciar buckets. Por exemplo, ao chamar a operação ListBucket, você pode filtrar a listagem para exibir apenas buckets com tags específicas.

Notas de uso

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região, use um endpoint interno. Para detalhes sobre regiões e endpoints compatíveis, consulte Regions and endpoints.

  • Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para mais informações, consulte 配置访问凭证.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Client configuration.

  • Cada bucket pode ter no máximo 20 tags (pares chave-valor).

  • Chaves e valores das tags devem estar codificados em UTF-8.

  • A chave de uma tag pode ter até 64 caracteres, diferencia maiúsculas de minúsculas e não pode estar vazia. Além disso, a chave não pode começar com http://, https:// ou Aliyun (sem distinção de maiúsculas e minúsculas).

  • O valor de uma tag pode ter até 128 caracteres e aceita string vazia.

Definir tags de bucket

O código a seguir mostra como definir tags para um bucket.

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
});

// Set bucket tags.
async function putBucketTags(bucketName, tag) {
  try {
    const result = await client.putBucketTags(bucketName, tag);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

const tag = { a: '1', b: '2' };
putBucketTags('yourBucketName', tag)

Obter tags de bucket

O código a seguir mostra como recuperar as tags de um bucket.

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
});

// Get bucket tags.
async function getBucketTags(bucketName) {
  try {
    const result = await client.getBucketTags(bucketName);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

getBucketTags('yourBucketName')

Listar buckets com tags específicas

O código a seguir ilustra como listar os buckets que possuem uma tag específica:

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',

});

// List buckets with specified tags.
async function listBucketTags() {
  try {
    const params = {
      // Add the tag-key and tag-value fields to the params parameter of the listBuckets operation.
      'tag-key': 'yourTagKey',
      'tag-value': 'yourTagValue'
    }
    const result = await client.listBuckets(params);
    console.log(result);
  } catch (err) {
    console.log(err);
  }
}

listBucketTags();

Excluir tags de bucket

O código a seguir demonstra como excluir as tags de um bucket.

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',

});

// Delete bucket tags.
async function deleteBucketTags(bucketName) {
  try {
    const result = await client.deleteBucketTags(bucketName);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteBucketTags('yourBucketName')

Referências

  • Para acessar o código de exemplo completo sobre tagging de bucket, consulte GitHub.

  • Para mais informações sobre a operação de API usada para definir tags de bucket, consulte PutBucketTags.

  • Para detalhes sobre a operação de API usada para recuperar tags de bucket, consulte GetBucketTags.

  • Para saber mais sobre a operação de API usada para excluir tags de bucket, consulte DeleteBucketTags.