本文介绍如何管理生命周期规则(Lifecycle)。

OSS支持设置生命周期(Lifecycle)规则,自动删除过期的文件和碎片,或将到期的文件转储为低频或归档存储类型,从而节省存储费用。每条规则包含:
  • 规则ID。用于标识一条规则,同一存储空间内规则ID不能重复。
  • 策略。有以下两种设置方式。同一存储空间内仅支持一种设置方式。
    • 按前缀匹配。此种方式允许创建多条规则,前缀不能重复。
    • 配置到整个存储空间。此种方式只能创建一条规则。
  • 过期时间。有两种指定方式:
    • 指定一个过期天数N,文件会在其最近更新时间点的N天后过期。
    • 指定一个过期时间点,最近更新时间在该时间点之前的文件全部过期。
  • 是否生效。

通过oss_upload_file方法上传的分片也支持设置生命周期规则。文件最后修改时间以初始化分片上传事件的时间为准。

更多关于生命周期的内容请参见管理对象生命周期

设置生命周期规则

以下代码用于设置生命周期规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    SetBucketLifecycleRequest request(BucketName);
    std::string date("2022-10-12T00:00:00.000Z");

    /* 指定过期天数 */
    auto rule1 = LifecycleRule();
    rule1.setID("rule1");
    rule1.setPrefix("test1/");
    rule1.setStatus(RuleStatus::Enabled);
    rule1.setExpiration(3);

    /* 设置转换存储类型的时间 */
    auto transition = LifeCycleTransition();
    transition.Expiration().setDays(180);
    transition.setStorageClass(StorageClass::IA);
    rule1.addTransition(transition);

    transition.Expiration().setDays(365);
    transition.setStorageClass(StorageClass::Archive);
    rule1.addTransition(transition);

    /* 设置生命周期规则 */
    LifecycleRuleList list{rule1};
    request.setLifecycleRules(list);
    auto outcome = client.SetBucketLifecycle(request);

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "SetBucketLifecycle fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

查看生命周期规则

以下代码用于查看生命周期规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, c onf);

    /* 查看生命周期规则 */
    auto outcome = client.GetBucketLifecycle(BucketName);

    if (outcome.isSuccess()) {
        std::cout << "GetBucketLifecycle success," << std::endl;
        for (auto const rule : outcome.result().LifecycleRules()) {
            std::cout << "rule:" << rule.ID() << "," << rule.Prefix() << "," << rule.Status() << ","
            "hasExpiration:" << rule.hasExpiration() << "," <<
            "hasTransitionList:" << rule.hasTransitionList() << "," << std::endl;
        }
    }
    else {
        /* 异常处理 */
        std::cout << "GetBucketLifecycle fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

清空生命周期规则

以下代码用于清空生命周期规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf);

    /* 清空生命周期规则 */
    DeleteBucketLifecycleRequest request(BucketName);
    auto outcome = client.DeleteBucketLifecycle(request);

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "DeleteBucketLifecycle fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}