edit-icon download-icon

Manage lifecycle rules

Last Updated: Nov 06, 2017

OSS allows you to set lifecycle rules for buckets to automatically remove expired objects and save the storage space. You can set multiple lifecycle rules. A lifecycle rule contains the following:

  • Rule ID: It is used to identify the rule and cannot be repeated.

  • Prefix of objects subject to the rule: The rule is only applicable to the objects with the specified prefix.

  • Expiration time: It can be specified in either of the following ways:

    • Specifying that the object expires on the Nth day after the last modification
    • Specifying that the object expires on a specific day. It means that all objects with the specified prefix expires after the day, regardless of the last modification time of the objects. This method is not recommended.
  • Take effect or not

For more information, see Manage object lifecycle.

Set lifecycle rules

The following code uses Bucket#lifecycle= to configure lifecycle rules:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket.lifecycle = [
  6. LifeCycleRule.new(
  7. :id => 'rule1', :enabled => true, :prefix => 'foo/', :expiry => 3),
  8. LifeCycleRule.new(
  9. :id => 'rule2', :enabled => false, :prefix => 'bar/', :expiry => Date.new(2016, 1, 1))
  10. ]

View lifecycle rules

The following code uses Bucket#lifecycle to display lifecycle rules:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. rules = bucket.lifecycle
  6. puts rules

Clear lifecycle rules

The following code uses Bucket#lifecycle= to configure an empty rule array for clearing lifecycle rules:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket.lifecycle = []
Thank you! We've received your feedback.