Manage lifecycle rules

Last Updated: Jun 05, 2017

The OSS provides object lifecycle management for you to manage objects. You can configure the lifecycle of a bucket to define various rules for the objects in the bucket.

Currently, you can define rules to delete specific objects. Each rule is composed of the following parts:

  • Object name prefix: The rule applies only to the objects whose names contain the specified prefix.
  • Operation: The operation to be performed on the objects that match the user-defined rule.
  • Date or number of days: The date when the specified operation will be performed on objects, or the number of days following the last object modification time after which the specified operation will be performed on objects.

Configure lifecycle rules

Lifecycle rules are configured in the XML format.

  1. <LifecycleConfiguration>
  2. <Rule>
  3. <ID>delete obsoleted files</ID>
  4. <Prefix>obsoleted/</Prefix>
  5. <Status>Enabled</Status>
  6. <Expiration>
  7. <Days>3</Days>
  8. </Expiration>
  9. </Rule>
  10. <Rule>
  11. <ID>delete temporary files</ID>
  12. <Prefix>temporary/</Prefix>
  13. <Status>Enabled</Status>
  14. <Expiration>
  15. <Date>2022-10-12T00:00:00.000Z</Date>
  16. </Expiration>
  17. </Rule>
  18. </LifecycleConfiguration>

The lifecycle configuration of a single object can contain up to 1,000 rules.

Explanations of fields:

  • The ID field uniquely identifies a rule.
  • The prefix field specifies the prefix that the names of objects in a bucket must contain to apply the rule. Duplicate prefixes are not allowed.
  • Status indicates the status of this rule. The statuses are either Enabled or Disabled, indicating if the rule is enabled or disabled.
  • In the Expiration node, Days indicates that an object will be deleted after the specified number of days after its last modification. Date indicates that an object will be deleted after the specified absolute time (the absolute time follows the ISO8601 format).

The following code configures the above mentioned lifecycle rules:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName);
  5. // Create the first rule
  6. LifecycleRule lcr1 = new LifecycleRule()
  7. {
  8. ID = "delete obsoleted files",
  9. Prefix = "obsoleted/",
  10. Status = RuleStatus.Enabled,
  11. ExpriationDays = 3
  12. };
  13. // Create the second rule
  14. // When a date is specified as ExpirationTime, it means that the rule will take effect and remain effective from the specified date on
  15. LifecycleRule lcr2 = new LifecycleRule()
  16. {
  17. ID = "delete temporary files",
  18. Prefix = "temporary/",
  19. Status = RuleStatus.Enabled,
  20. ExpirationTime = DateTime.Parse("2022-10-12T00:00:00.000Z")
  21. };
  22. setBucketLifecycleRequest.AddLifecycleRule(lcr1);
  23. setBucketLifecycleRequest.AddLifecycleRule(lcr2);
  24. client.SetBucketLifecycle(setBucketLifecycleRequest);

Complete code can be found at: GitHub.

Note:

  • In the above first rule, a days value is used in the ExpirationDays, indicating that the rule will take effect three days later and remain effective since then.
  • In the above second rule, a date value is used in the ExpirationDays, indicating that the rule will take effect after “2022-10-12T00:00:00.000Z” and remain effective since then. Use the Date format with cautions unless you have a full understanding of its meaning.

Obtain lifecycle rules

The following code obtains the above two lifecycle rules:

  1. using Aliyun.OSS;
  2. var rules = client.GetBucketLifecycle(bucketName);
  3. foreach (var rule in rules)
  4. {
  5. Console.WriteLine("ID: {0}", rule.ID);
  6. Console.WriteLine("Prefix: {0}", rule.Prefix);
  7. Console.WriteLine("Status: {0}", rule.Status);
  8. if (rule.ExpriationDays.HasValue)
  9. Console.WriteLine("ExpirationDays: {0}", rule.ExpriationDays);
  10. if (rule.ExpirationTime.HasValue)
  11. Console.WriteLine("ExpirationTime: {0}", FormatIso8601Date(rule.ExpirationTime.Value));
  12. }

Note: Complete code can be found at: GitHub

Clear lifecycle rules

The following code clears the lifecycle rules of a bucket.

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. var LifecycleRequest = new SetBucketLifecycleRequest(bucketName);
  5. client.SetBucketLifecycle(LifecycleRequest);
Thank you! We've received your feedback.