OSS提供标准、低频访问、归档三种存储类型,全面覆盖从热到冷的各种数据存储场景。本文主要介绍如何转换文件(Object)的存储类型。

有关标准存储、低频访问、归档存储三种存储类型的更多信息,请参见开发指南的存储类型介绍存储类型转换

以下提供了详细的示例代码用于Object存储类型的相互转换。
  • 以下代码用于将Object的存储类型从标准或低频访问转换为归档类型:
    #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 SourceBucketName = "yourSourceBucketName";
        std::string SourceObjectName = "yourSourceObjectName";
    
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /* 设置修改后的文件存储类型,例如将修改后的文件存储类型设置为归档 */
        ObjectMetaData objectMeta;
        objectMeta.addHeader("x-oss-storage-class", "Archive");
        CopyObjectRequest request(SourceBucketName, SourceBucketName,objectMeta);
        request.setCopySource(SourceBucketName, SourceObjectName);
    
        /* 修改为上述指定的文件存储类型 */
        auto outcome = client.CopyObject(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "CopyObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 以下代码用于将Object的存储类型从归档转换为低频访问类型:
    #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 SourceBucketName = "yourSourceBucketName";
        std::string SourceObjectName = "yourSourceObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /* 检查目标文件是否为归档类型。如果是,则需要先解冻才能更改存储类型 */
        auto restoreOutcome = client.RestoreObject(SourceBucketName, SourceObjectName);
        if (!restoreOutcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "RestoreObject fail" <<
            ",code:" << restoreOutcome.error().Code() <<
            ",message:" << restoreOutcome.error().Message() <<
            ",requestId:" << restoreOutcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        std::string onGoingRestore("ongoing-request=\"false\"");
    
        int maxWaitTimeInSeconds = 600;
        while (maxWaitTimeInSeconds > 0)
        {
            auto meta = client.HeadObject(SourceBucketName, SourceObjectName);
    
            std::string restoreStatus = meta.result().HttpMetaData()["x-oss-restore"];
            std::transform(restoreStatus.begin(), restoreStatus.end(), restoreStatus.begin(), ::tolower);
            if (!restoreStatus.empty() && 
            restoreStatus.compare(0, onGoingRestore.size(), onGoingRestore)==0) {
                std::cout << " success, restore status:" << restoreStatus << std::endl;
                /* 成功解冻归档文件*/
                break;
            }
    
            std::cout << " info, WaitTime:" << maxWaitTimeInSeconds
            << "; restore status:" << restoreStatus << std::endl;
    
            std::this_thread::sleep_for(std::chrono::seconds(10));
            maxWaitTimeInSeconds--;     
        }
    
        /* 设置修改后的文件存储类型,例如将修改后的文件存储类型设置为低频 */
        ObjectMetaData objectMeta;
        objectMeta.addHeader("x-oss-storage-class", "IA");
        CopyObjectRequest request(SourceBucketName, SourceBucketName,objectMeta);
        request.setCopySource(SourceBucketName, SourceObjectName);
    
        /* 修改为上述指定的文件存储类型 */
        auto outcome = client.CopyObject(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "CopyObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }

各种存储类型的存储费用介绍,请参见计量项和计费项的存储费用一节。