全部產品
Search
文件中心

Object Storage Service:C++生命週期

更新時間:Jun 19, 2024

並不是所有上傳至OSS的資料都需要頻繁訪問,但基於資料合規或者存檔等原因,部分資料仍需要繼續以冷儲存類型進行儲存。或者基於業務使用情境,希望大量刪除Bucket內不再需要儲存的資料。您可以配置基於最後一次修改時間(Last Modified Time)的生命週期規則,定期將Object從熱儲存類型轉為冷儲存類型或者刪除Object,以降低儲存成本。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見訪問網域名稱和資料中心

  • 本文以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";
    /*填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";

    /*初始化網路等資源。*/
    InitializeSdk();

    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    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";
    /*填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";
  
    /*初始化網路等資源。*/
    InitializeSdk();
  
    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
  
    /*查看生命週期規則。*/
    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";
    /*填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";
  
    /*初始化網路等資源。*/
    InitializeSdk();
  
    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, 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;
        return -1;
    }
    /*釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

相關文檔