All Products
Search
Document Center

Object Storage Service:Restore objects

Last Updated:Sep 11, 2023

You must restore an Archive object or a Cold Archive object before you can read the object. This topic describes how to restore an Archive object or a Cold Archive object.

Usage notes

  • The RestoreObject operation can be called only on Archive and Cold Archive objects.

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To restore objects, you must have the oss:RestoreObject permission. For more information, see Common examples of RAM policies.

Restore an Archive object

The following code provides an example on how to restore an Archive object:

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

using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the Archive object. Do not include the bucket name in the full path. */
    std::string ObjectName = "yourObjectName";
  
    /* Initialize resources such as network resources. */
    InitializeSdk();
    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
    /* Restore the Archive object. */
    auto outcome = client.RestoreObject(BucketName, ObjectName);
    /* You cannot restore objects of the non-Archive storage class. */
    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        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;
            /* The Archive object is restored. */
            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;
    }
    /* Release resources such as network resources. */
    ShutdownSdk();
    return 0;
}

Restore a Cold Archive object

The following code provides an example on how to restore a Cold Archive object:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the Cold Archive object. Do not include the bucket name in the full path. */
    std::string ObjectName = "yourObjectName";

    /* Initialize resources such as network resources. */
    InitializeSdk();

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

    /* Refer to the following code if you set the storage class of the object that you want to upload to Cold Archive. */
    //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);
    /* Specify the duration for which the object can remain in the restored state. Unit: days. Default value: 1. */
    request.setDays(2);
    /* Set the restoration priority of the Cold Archive object. TierType::Expedited indicates that the object is restored within 1 hour. */
    request.setTierType(TierType::Expedited);
    auto outcome = client.RestoreObject(request);

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

    /* Release resources such as network resources. */
    ShutdownSdk();
    return 0;
}

References

  • For the complete sample code that is used to restore Archive and Cold Archive objects, visit GitHub.

  • For more information about the API operation that you can call to restore Archive objects and Cold Archive objects, see RestoreObject.