When you configure lifecycle rules, you can specify conditions for the lifecycle rules to take effect. Lifecycle rules can take effect on objects with specified prefixes or tags. You can also specify combinations of prefixes and tags as conditions of lifecycle rules at the same time.

Note If you set a specified tag as the condition of a lifecycle rule, the rule applies to an object only when the key and value in the tag of the object both match the specified tag. If you specify a prefix and multiple tags as the condition of a lifecycle rule, the rule applies to an object only when the prefix and tags of the object match the prefix and all tags specified in the rule.

Set tags as the matching condition of a lifecycle rule

The following code provides an example on how to set tags as the matching condition of a lifecycle rule:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // This example uses the China (Hangzhou) region. Specify the actual region 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 the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Set tags as the matching condition.
const tag = [{
  key: 'key1',
  value: 'value1'
},{
  key: 'key2',
  value: 'value2'
}]

client.putBucketLifecycle('<yourBucketName>', [{
  // Set the lifecycle rule ID to rule1.
  id: 'rule1', 
  // Specify to apply the lifecycle rule to objects whose names are prefixed with one.
  prefix: 'one', 
  status: 'Enabled',
  expiration: {
  // Specify a lifecycle rule for an object so that the object expires 60 days after it was last modified.
    days: 60 
  },
  // Specify a lifecycle rule for an object so that the storage class of the object is converted to IA 10 days after it was last modified.
  transition: { 
    days: 10,
    StorageClass: 'IA'
  },
  tag
}]);

View the tags set as the matching condition of a lifecycle rule

The following code provides an example on how to view the tags set as the matching condition of a lifecycle rule:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // 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 the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});


try {
  const result = client.getBucketLifecycle('<yourBucketName>')

  // View the tags that are set as the matching condition for a lifecycle rule.
  result.rules.map(rule => {
    if (rule.tag) {
      rule.tag.map(_ => {
        console.log(`key: ${_.key}, value: ${_.value}`)
      })
    }
  })
} catch (error) {
  console.log(error)
}