edit-icon download-icon

Manage lifecycle rules

Last Updated: Dec 22, 2017

OSS allows you to set lifecycle rules for buckets to automatically remove expired objects and save bucket spaces. For more information about lifecycle, see Manage object lifecycle.

Set lifecycle rules

You can use the oss_put_bucket_lifecycle interface to set the lifecycle rules.

The lifecycle configuration rules are expressed by an XML segment.

  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. </LifecycleConfiguration>

Field description:

  • The ID field is used to uniquely identify a rule (including relations, such as abc and abcd, cannot exist between IDs).

  • Prefix indicates the rules apply to the objects that contain the specified prefix in name in the bucket.

  • Status indicates the status of this rule. The status is either Enabled or Disabled.

  • In the Expiration node, Days indicate that an object is deleted after the specified number of days are completed when last modification. Date indicates that an object is deleted after the specified absolute time (the absolute time follows the ISO8601 format).

Using the following code, you can set the preceding lifecycle rules:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *resp_headers;
  5. char *bucket_name = "<Name of your bucket>";
  6. aos_string_t bucket;
  7. aos_list_t lifecycle_rule_list;
  8. oss_lifecycle_rule_content_t *rule_content;
  9. char *rule_name = "rule_name";
  10. aos_pool_create(&p, NULL);
  11. /* Create and initialize options */
  12. options = oss_request_options_create(p);
  13. init_options(options);
  14. /* Create a lifecycle rule and apply it to a bucket */
  15. aos_str_set(&bucket, bucket_name);
  16. aos_list_init(&lifecycle_rule_list);
  17. rule_content = oss_create_lifecycle_rule_content(p);
  18. aos_str_set(&rule_content->id, rule_name);
  19. aos_str_set(&rule_content->prefix, "obsoleted");
  20. aos_str_set(&rule_content->status, "Enabled");
  21. rule_content->days = 3;
  22. aos_list_add_tail(&rule_content->node, &lifecycle_rule_list);
  23. s = oss_put_bucket_lifecycle(options, &bucket, &lifecycle_rule_list, &resp_headers);
  24. if (aos_status_is_ok(s)) {
  25. printf("put bucket lifecycle succeeded\n");
  26. } else {
  27. printf("put bucket lifecycle failed\n");
  28. }
  29. aos_pool_destroy(p);

View lifecycle rules

You can use the oss_get_bucket_lifecycle interface to view the lifecycle rules:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *resp_headers;
  5. char *bucket_name = "<Name of your bucket>";
  6. aos_string_t bucket;
  7. aos_list_t lifecycle_rule_list;
  8. oss_lifecycle_rule_content_t *rule_content;
  9. char *rule_id;
  10. char *prefix;
  11. char *status;
  12. int days = INT_MAX;
  13. char* date = "";
  14. aos_pool_create(&p, NULL);
  15. /* Create and initialize options */
  16. options = oss_request_options_create(p);
  17. init_options(options);
  18. /* Get the bucket lifecycle rules and print the rules */
  19. aos_str_set(&bucket, bucket_name);
  20. aos_list_init(&lifecycle_rule_list);
  21. s = oss_get_bucket_lifecycle(options, &bucket, &lifecycle_rule_list, &resp_headers);
  22. aos_list_for_each_entry(rule_content, &lifecycle_rule_list, node) {
  23. rule_id = apr_psprintf(p, "%.*s", rule_content->id.len, rule_content->id.data);
  24. prefix = apr_psprintf(p, "%.*s", rule_content->prefix.len, rule_content->prefix.data);
  25. status = apr_psprintf(p,"%.*s", rule_content->status.len, rule_content->status.data);
  26. date = apr_psprintf(p, "%.*s", rule_content->date.len, rule_content->date.data);
  27. days = rule_content->days;
  28. }
  29. aos_pool_destroy(p);

Clear lifecycle rules

You can use the oss_delete_bucket_lifecycle interface to clear the lifecycle rules:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *resp_headers;
  5. char *bucket_name = "<Name of your bucket>";
  6. aos_string_t bucket;
  7. aos_pool_create(&p, NULL);
  8. /* Create and initialize options */
  9. options = oss_request_options_create(p);
  10. init_options(options);
  11. /* Delete the bucket lifecycle rules */
  12. aos_str_set(&bucket, bucket_name);
  13. s = oss_delete_bucket_lifecycle(options, &bucket, &resp_headers);
  14. if (aos_status_is_ok(s)) {
  15. printf("delete bucket lifecycle succeeded\n");
  16. } else {
  17. printf("delete bucket lifecycle failed\n");
  18. }
  19. aos_pool_destroy(p);
Thank you! We've received your feedback.