edit-icon download-icon

Manage lifecycle rules

Last Updated: Oct 27, 2017

OSS provides object lifecycle management to help the user 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 a 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 operations are to be performed on objects, or the number of days following the last object modification time after which the specified operation is to 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.

Description of the fields:

  • The ID field uniquely identifies a rule.

  • The prefix field is the name of the objects in a bucket must contain to apply the rule. Duplicate prefixes are not allowed.

  • Status indicates the status of this rule. It can be Enabled or Disabled, indicating if the rule is enabled or disabled.

  • In the Expiration node, Days indicates that an object gets deleted post expiry of the specified number of days from its last modification time. Date indicates that an object is to be deleted after the specified absolute time (the absolute time follows the ISO8601 format).

The following code configures the preceding 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 takes 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);

For complete code, see GitHub.

Note:

  • In the preceding first rule, a day’s value is used in the ExpirationDays, indicating the rule takes effect three days later and remains effective from that duration.

  • The second rule states, a date value is used in the ExpirationDays, indicating that the rule takes effect after “2022-10-12T00:00:00.000Z” and remains effective from that duration. Use the Date format carefully.

Obtain lifecycle rules

The following code obtains the preceding two 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. }

For complete code, see 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.