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

Note 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 tagging configurations to a lifecycle rule

The following code provides an example on how to add tagging configurations to a lifecycle rule:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname'
});

// 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', 
  // Apply the lifecycle rule to objects whose names are prefixed with one. 
  prefix: 'one', 
  status: 'Enabled',
  expiration: {
  // Configure the lifecycle rule so that the objects expire 60 days after they are last modified. 
    days: 60 
  },
  // Configure the lifecycle rule so that the storage class of the objects is converted to Infrequent Access (IA) 10 days after the objects are last modified. 
  transition: { 
    days: 10,
    StorageClass: 'IA'
  },
  tag
}]);

Query the tagging configurations in a lifecycle rule

The following code provides an example on how to query tagging configurations in a lifecycle rule:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify yourbucketname as the name of the bucket. 
  bucket: 'yourbucketname'
});


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

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

References

  • For more information about the complete sample code for lifecycle rules, visit GitHub.
  • For more information about the API operation that you can call to configure lifecycle rules, see PutBucketLifecycle.
  • For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.