全部產品
Search
文件中心

Object Storage Service:C++解凍檔案

更新時間:Sep 25, 2024

歸檔或冷歸檔類型的Object需要解凍(Restore)之後才能讀取。本文介紹如何解凍歸檔和冷歸檔Object。

注意事項

  • 僅歸檔、冷歸檔類型的Object支援調用RestoreObject方法。

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

  • 本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OssClient

  • 要解凍檔案,您必須有oss:RestoreObject許可權。具體操作,請參見為RAM使用者授權自訂的權限原則

解凍歸檔Object

以下代碼用於解凍歸檔Object:

#include <alibabacloud/oss/OssClient.h>
#include <thread>
#include <chrono>
#include <algorithm>

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";
    /*填寫不包含Bucket名稱在內的歸檔類型Object的完整路徑。*/
    std::string ObjectName = "yourObjectName";
  
    /*初始化網路等資源。*/
    InitializeSdk();
    
    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
  
    /*解凍歸檔Object。*/
    auto outcome = client.RestoreObject(BucketName, ObjectName);
    /*非歸檔Object不能解凍。*/
    if (!outcome.isSuccess()) {
        /*異常處理。*/
        std::cout << "RestoreObject fail,code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }
    std::string onGoingRestore("ongoing-request=\"false\"");
    int maxWaitTimeInSeconds = 600;
    while (maxWaitTimeInSeconds > 0)
    {
        auto meta = client.HeadObject(BucketName, ObjectName);
        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;
            /*成功解凍歸檔Object。*/
            break;
        }
        std::cout << " info, WaitTime:" << maxWaitTimeInSeconds
        << "; restore status:" << restoreStatus << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(10));
        maxWaitTimeInSeconds--;     
    }
    if (maxWaitTimeInSeconds == 0)
    {
        std::cout << "RestoreObject fail, TimeoutException" << std::endl;
    }
    /*釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

解凍冷歸檔Object

以下代碼用於解凍冷歸檔Object:

#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";
    /*填寫不包含Bucket名稱在內的冷歸檔類型Object的完整路徑。*/
    std::string ObjectName = "yourObjectName";

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

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

    /*如果上傳檔案的同時指定檔案儲存體類型為冷歸檔,請參考如下代碼。*/
    //auto content = std::make_shared<std::stringstream>("test");
    //PutObjectRequest putRequest(BucketName, ObjectName, content);
    //putRequest.MetaData().addHeader("x-oss-storage-class", "ColdArchive");
    //auto putOutcome = client.PutObject(putRequest);

    RestoreObjectRequest request(BucketName, ObjectName);
    /*設定解凍之後保持解凍狀態的天數,預設值為1天。*/
    request.setDays(2);
    /*設定解凍冷歸檔類型檔案的優先順序。TierType::Bulk表示5~12個小時之內解凍完成。*/
    request.setTierType(TierType::Bulk);
    auto outcome = client.RestoreObject(request);

    if (!outcome.isSuccess()) {
        /*異常處理。*/
        std::cout << "Delete Bucket Inventory fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
    }

    /*釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

相關文檔

  • 關於解凍歸檔和冷歸檔類型Object的完整範例程式碼,請參見GitHub樣本

  • 關於解凍Object的API介面說明,請參見RestoreObject