By using the bucket tagging function, you can classify your buckets to manage them. For example, you can specify that only buckets with specified tags are returned in ListBucket operations.

  • Only the bucket owner and authorized RAM users can add tags to a bucket. Otherwise, the 403 Forbidden error is returned with the error code: AccessDenied.
  • You can add a maximum of 20 tags (key-value pairs) to a bucket.
  • The maximum size of a key is 64 bytes. The key of a tag cannot be null and cannot be prefixed with http://, https://, or Aliyun.
  • The maximum size of a tag value is 128 bytes. The value of a tag can be null.
  • The key and value of a tag must be UTF-8 encoded.
  • If you use PutBucketTagging to add tags to a bucket, the original tags added to the bucket are completely overwritten.

Configure bucket tagging

You can run the following code to add tags to a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Configure bucket tagging.
async function putBucketTags(bucketName, tag) {
  try {
    let result = await client.putBucketTags(bucketName, tag);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

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

Query bucket tags

You can run the following code to query the tags to a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  //This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

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

getBucketTags('bucketName')

Lists buckets that have specific tags

You can run the following code to list buckets with a specified tag:

const OSS = require('ali-oss')

const client = new OSS({
  //This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your region>',
  //Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

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

listBucketTags('bucketName')

Delete bucket tagging configurations

You can run the following code to delete the tags added to a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  //This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your region>',
  //Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

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

deleteBucketTags('bucketName')