Bucket tagging (Node.js SDK)
Bucket tags allow you to classify and manage buckets. For example, you can specify to list only buckets with specific tags when you call the ListBucket operation.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
-
In this topic, access credentials are obtained from environment variables. For more information, see Configure access credentials.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Client configuration.
Each bucket can have up to 20 tags (key-value pairs).
Tag keys and values must be UTF-8 encoded.
A tag key can be up to 64 characters in length, is case-sensitive, and cannot be empty. The key cannot start with
http://,https://, orAliyun(case-insensitive).A tag value can be up to 128 characters in length and can be an empty string.
Set bucket tags
The following code provides an example of how to set tags for a 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)
Get bucket tags
The following code provides an example of how to retrieve the tags of a 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')
List buckets with specified tags
The following code provides an example on how to list the buckets with a specific tag:
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();
Delete bucket tags
The following code provides an example of how to delete the tags of a 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')
References
-
For the complete sample code for bucket tagging, see GitHub.
-
For more information about the API operation used to set bucket tags, see PutBucketTags.
-
For more information about the API operation used to retrieve bucket tags, see GetBucketTags.
-
For more information about the API operation used to delete bucket tags, see DeleteBucketTags.