All Products
Search
Document Center

Object Storage Service:Use object tagging for lifecycle management

Last Updated:Jan 09, 2024

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.

Specify tags in a lifecycle rule

The following sample code provides an example on how to specify tags 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',
  // Obtain access credentials from environment variables. Before you run the sample code, 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,
  // Specify the name of the 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 tags configured in a lifecycle rule

The following sample code provides an example on how to query tags configured 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',
  // Obtain access credentials from environment variables. Before you run the sample code, 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,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

async function getBucketLifecycle() {
  try {
    const result = await client.getBucketLifecycle('peiyu-demo-2s')
    console.log(result)

    // View tags specified in 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 that is used to manage lifecycle rules, visit GitHub.

  • For more information about the API operation that you can call to configure a lifecycle rule, see PutBucketLifecycle.

  • For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.