Object tagging and lifecycle management (Node.js SDK)
When you configure a lifecycle rule, you can specify the conditions that trigger the rule. A lifecycle rule can apply to objects based on a prefix, an object tag, or both.
If you configure tag conditions, the rule applies only to objects that meet the tag key and value conditions. If a prefix and multiple object tags are configured in a rule, the rule applies only to objects that match the prefix and object tag conditions.
Add a tag-based matching rule to a lifecycle rule
The following code shows how to add a tag-based matching rule to a lifecycle rule:
const OSS = require('ali-oss')
const client = new OSS({
// Replace yourregion with 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 you have set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Replace yourbucketname with the bucket name.
bucket: 'yourbucketname'
});
// Set the tags to match.
const tag = [{
key: 'key1',
value: 'value1'
},{
key: 'key2',
value: 'value2'
}]
client.putBucketLifecycle('yourBucketName', [{
// Specify the lifecycle rule ID as rule1.
id: 'rule1',
// Apply the rule to objects with the prefix "one".
prefix: 'one',
status: 'Enabled',
expiration: {
// Specify that objects expire 60 days after they are last modified.
days: 60
},
// Specify that objects are converted to the Infrequent Access (IA) storage class 10 days after they are last modified.
transition: {
days: 10,
storageClass: 'IA'
},
tag
}]);
View tag information in a lifecycle rule
The following code shows how to view the tag information in a lifecycle rule:
const OSS = require('ali-oss')
const client = new OSS({
// Set yourregion to the region where your bucket is located. For example, if your 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 sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set yourbucketname to the name of your bucket.
bucket: 'yourbucketname'
});
async function getBucketLifecycle() {
try {
const result = await client.getBucketLifecycle('peiyu-demo-2s')
console.log(result)
// View the information about the tags that are matched by the lifecycle rule.
if (result.rules) {
result.rules.forEach(rule => {
if (rule.tag) {
rule.tag.forEach(tag => {
console.log(`key: ${tag.key}, value: ${tag.value}`)
})
}
})
}
} catch (error) {
console.log(error)
}
}
getBucketLifecycle();
References
-
For the complete sample code for lifecycle rules, see the GitHub example.
-
For a description of the API operation for setting lifecycle rules, see PutBucketLifecycle.
-
For a description of the API operation for viewing lifecycle rules, see GetBucketLifecycle.