全部產品
Search
文件中心

Object Storage Service:生命週期管理

更新時間:Aug 30, 2018

管理生命週期(Lifecycle)

OSS允許使用者對儲存空間設定生命週期規則,以自動淘汰過期掉的檔案,節省儲存空間。更多關於生命週期的內容請參考 檔案生命週期

設定生命週期規則

通過oss_put_bucket_lifecycle介面,可以實現設定生命週期規則:

lifecycle的配置規則由一段xml表示。

  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>

各欄位解釋:

  • ID欄位是用來唯一表示本條Rule(各個ID之間不能由內含項目關聯性,比如abc和abcd這樣的)。
  • Prefix指定對bucket下的符合特定首碼的檔案使用規則。
  • Status指定本條規則的狀態,只有Enabled和Disabled,分別表示啟用規則和禁用規則。
  • Expiration節點裡面的Days表示大於檔案最後修改時間指定的天數就刪除檔案,Date則表示到指定的絕對時間之後就刪除檔案(絕對時間服從ISO8601的格式)。

可以通過下面的代碼,設定上述lifecycle規則。

  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 = "<您的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. /* 建立並初始化options */
  12. options = oss_request_options_create(p);
  13. init_options(options);
  14. /* 建立生命週期規則並設定給儲存空間 */
  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);

查看生命週期規則

通過oss_get_bucket_lifecycle介面,可以實現查看生命週期規則:

  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 = "<您的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. /* 建立並初始化options */
  16. options = oss_request_options_create(p);
  17. init_options(options);
  18. /* 獲取儲存空間生命週期規則並列印 */
  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);

清空生命週期規則

通過oss_delete_bucket_lifecycle介面,實現清空生命週期規則:

  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 = "<您的bucket名字>";
  6. aos_string_t bucket;
  7. aos_pool_create(&p, NULL);
  8. /* 建立並初始化options */
  9. options = oss_request_options_create(p);
  10. init_options(options);
  11. /* 刪除儲存空間生命週期規則 */
  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);