全部产品
Search
文档中心

对象存储 OSS:C++生命周期

更新时间:Nov 15, 2024

并不是所有上传至OSS的数据都需要频繁访问,但基于数据合规或者存档等原因,部分数据仍需要继续以冷存储类型进行保存。或者基于业务使用场景,希望批量删除Bucket内不再需要保存的数据。您可以配置基于最后一次修改时间(Last Modified Time)的生命周期规则,定期将Object从热存储类型转为冷存储类型或者删除Object,以降低存储成本。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见OSS地域和访问域名

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OssClient

  • 要设置生命周期规则,您必须有oss:PutBucketLifecycle权限;要查看生命周期规则,您必须有oss:GetBucketLifecycle权限;要清空生命周期规则,您必须有oss:DeleteBucketLifecycle权限。具体操作,请参见为RAM用户授权自定义的权限策略

设置生命周期规则

以下代码用于为examplebucket设置基于最后一次修改时间策略的生命周期规则。设置完成后,如果您希望修改其中的一条或多条生命周期规则,请参见如何修改其中一条或多条生命周期规则配置?

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

int main(void)
{
    /*初始化OSS账号信息。*/
    
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* yourRegion填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。*/
    std::string Region = "yourRegion";
    /*填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

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

    /*设置标签。*/
    Tagging tagging;
    tagging.addTag(Tag("key1", "value1"));
    tagging.addTag(Tag("key2", "value2"));

    /*指定生命周期规则。*/
    auto rule1 = LifecycleRule();
    rule1.setID("rule1");
    rule1.setPrefix("test1/");
    rule1.setStatus(RuleStatus::Enabled);
    rule1.setExpiration(3);
    rule1.setTags(tagging.Tags());

    /*指定过期时间。*/
    auto rule2 = LifecycleRule();
    rule2.setID("rule2");
    rule2.setPrefix("test2/");
    rule2.setStatus(RuleStatus::Disabled);
    rule2.setExpiration(date);

    /*rule3为针对版本控制状态下的Bucket的生命周期规则。*/
    auto rule3 = LifecycleRule();
    rule3.setID("rule3");
    rule3.setPrefix("test3/");
    rule3.setStatus(RuleStatus::Disabled);

    /*设置Object距其最后修改时间365天之后自动转为归档类型。*/  
    auto transition = LifeCycleTransition();  
    transition.Expiration().setDays(365);
    transition.setStorageClass(StorageClass::Archive);
    rule3.addTransition(transition);

    /*设置自动移除过期删除标记。*/
    rule3.setExpiredObjectDeleteMarker(true);

    /*设置非当前版本的Object距最后修改时间10天之后转为低频访问类型。*/
    auto transition1 = LifeCycleTransition();  
    transition1.Expiration().setDays(10);
    transition1.setStorageClass(StorageClass::IA);

    /*设置非当前版本的Object距最后修改时间20天之后转为归档类型。*/
    auto transition2 = LifeCycleTransition();  
    transition2.Expiration().setDays(20);
    transition2.setStorageClass(StorageClass::Archive);

    /*设置Object在其成为非当前版本30天之后删除。*/
    auto expiration  = LifeCycleExpiration(30);
    rule3.setNoncurrentVersionExpiration(expiration);

    LifeCycleTransitionList noncurrentVersionStorageTransitions{transition1, transition2};
    rule3.setNoncurrentVersionTransitionList(noncurrentVersionStorageTransitions);

    /*设置生命周期规则。*/
    LifecycleRuleList list{rule1, rule2, rule3};
    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;
        return -1;
    }

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

查看生命周期规则

以下代码用于查看examplebucket的生命周期规则。

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
std::string ToStorageClassName(const StorageClass& storageClass) {
    switch (storageClass) {
        case StorageClass::Standard:
            return "Standard";
        case StorageClass::IA:
            return "IA";
        case StorageClass::Archive:
            return "Archive";
        case StorageClass::ColdArchive:
            return "ColdArchive";
        default:
            return "Unknown";
    }
}
int main(void)
{
    /*初始化OSS账号信息。*/
    
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* yourRegion填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。*/
    std::string Region = "yourRegion";
    /*填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
  
    /*初始化网络等资源。*/
    InitializeSdk();
  
    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
    /*查看生命周期规则。*/
    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;
            auto taglist = rule.Tags();
            for (const auto& tag : taglist)
            {
                std::cout << "GetBucketLifecycle tag success, Key:" 
                << tag.Key() << "; Value:" << tag.Value() << std::endl;
            }
            /*查看是否自动删除过期删除标记。*/
            if (rule.ExpiredObjectDeleteMarker()) {
                std::cout << "rule expired delete marker: " << rule.ExpiredObjectDeleteMarker() << std::endl;
            }
            /*查看非当前版本Object存储类型转换规则。*/
            if (rule.hasNoncurrentVersionTransitionList()) {
                for (auto const lifeCycleTransition : rule.NoncurrentVersionTransitionList()) {
                    std::cout << "rule noncurrent versions trans days:" << std::to_string(lifeCycleTransition.Expiration().Days()) <<
                    " trans storage class: " << ToStorageClassName(lifeCycleTransition.StorageClass()) << std::endl;    
                }
            }
            /*查看非当前版本Object过期规则。*/
            if (rule.hasNoncurrentVersionExpiration()) {
                std::cout << "rule noncurrent versions expiration days:" << rule.NoncurrentVersionExpiration().Days() << std::endl;
            }
        }
    }
    else {
        /*异常处理。*/
        std::cout << "GetBucketLifecycle fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }
    /* 释放网络等资源。*/
    ShutdownSdk();
    return 0;
}

清空生命周期规则

以下代码用于清空examplebucket的所有生命周期规则。如果您需要删除其中一条或者多条生命周期规则,请参见如何删除其中一条或多条生命周期规则?

#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{
    /*初始化OSS账号信息。*/
    
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* yourRegion填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。*/
    std::string Region = "yourRegion";
    /*填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
  
    /*初始化网络等资源。*/
    InitializeSdk();
  
    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
    /*清空生命周期规则。*/
    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;
        return -1;
    }
    /*释放网络等资源。*/
    ShutdownSdk();
    return 0;
}

相关文档