OSS允许用户对存储空间设置生命周期规则,以自动淘汰过期掉的文件,节省存储空间。 更多关于生命周期的内容请参考
文件生命周期
设置生命周期规则
通过oss_put_bucket_lifecycle接口,可以实现设置生命周期规则:
lifecycle的配置规则由一段xml表示。<LifecycleConfiguration><Rule><ID>delete obsoleted files</ID><Prefix>obsoleted/</Prefix><Status>Enabled</Status><Expiration><Days>3</Days></Expiration></Rule></LifecycleConfiguration>
各字段解释:
- ID字段是用来唯一表示本条Rule(各个ID之间不能由包含关系,比如abc和abcd这样的)。
- Prefix指定对bucket下的符合特定前缀的文件使用规则。
- Status指定本条规则的状态,只有Enabled和Disabled,分别表示启用规则和禁用规则。
- Expiration节点里面的Days表示大于文件最后修改时间指定的天数就删除文件,Date则表示到指定的绝对时间之后就删除文件(绝对时间服从ISO8601的格式)。
可以通过下面的代码,设置上述lifecycle规则。aos_pool_t *p;oss_request_options_t *options;aos_status_t *s;aos_table_t *resp_headers;char *bucket_name = "<您的bucket名字>";aos_string_t bucket;aos_list_t lifecycle_rule_list;oss_lifecycle_rule_content_t *rule_content;char *rule_name = "rule_name";aos_pool_create(&p, NULL);/* 创建并初始化options */options = oss_request_options_create(p);init_options(options);/* 创建生命周期规则并设置给存储空间 */aos_str_set(&bucket, bucket_name);aos_list_init(&lifecycle_rule_list);rule_content = oss_create_lifecycle_rule_content(p);aos_str_set(&rule_content->id, rule_name);aos_str_set(&rule_content->prefix, "obsoleted");aos_str_set(&rule_content->status, "Enabled");rule_content->days = 3;aos_list_add_tail(&rule_content->node, &lifecycle_rule_list);s = oss_put_bucket_lifecycle(options, &bucket, &lifecycle_rule_list, &resp_headers);if (aos_status_is_ok(s)) {printf("put bucket lifecycle succeeded\n");} else {printf("put bucket lifecycle failed\n");}aos_pool_destroy(p);
查看生命周期规则
通过oss_get_bucket_lifecycle接口,可以实现查看生命周期规则:aos_pool_t *p;oss_request_options_t *options;aos_status_t *s;aos_table_t *resp_headers;char *bucket_name = "<您的bucket名字>";aos_string_t bucket;aos_list_t lifecycle_rule_list;oss_lifecycle_rule_content_t *rule_content;char *rule_id;char *prefix;char *status;int days = INT_MAX;char* date = "";aos_pool_create(&p, NULL);/* 创建并初始化options */options = oss_request_options_create(p);init_options(options);/* 获取存储空间生命周期规则并打印 */aos_str_set(&bucket, bucket_name);aos_list_init(&lifecycle_rule_list);s = oss_get_bucket_lifecycle(options, &bucket, &lifecycle_rule_list, &resp_headers);aos_list_for_each_entry(rule_content, &lifecycle_rule_list, node) {rule_id = apr_psprintf(p, "%.*s", rule_content->id.len, rule_content->id.data);prefix = apr_psprintf(p, "%.*s", rule_content->prefix.len, rule_content->prefix.data);status = apr_psprintf(p,"%.*s", rule_content->status.len, rule_content->status.data);date = apr_psprintf(p, "%.*s", rule_content->date.len, rule_content->date.data);days = rule_content->days;}aos_pool_destroy(p);
清空生命周期规则
通过oss_delete_bucket_lifecycle接口,实现清空生命周期规则:aos_pool_t *p;oss_request_options_t *options;aos_status_t *s;aos_table_t *resp_headers;char *bucket_name = "<您的bucket名字>";aos_string_t bucket;aos_pool_create(&p, NULL);/* 创建并初始化options */options = oss_request_options_create(p);init_options(options);/* 删除存储空间生命周期规则 */aos_str_set(&bucket, bucket_name);s = oss_delete_bucket_lifecycle(options, &bucket, &resp_headers);if (aos_status_is_ok(s)) {printf("delete bucket lifecycle succeeded\n");} else {printf("delete bucket lifecycle failed\n");}aos_pool_destroy(p);