tencent cloud

Cloud Object Storage

POST Object restore

PDF
Focus Mode
Font Size
Last updated: 2026-03-05 17:02:06

Introduction

This document introduces the sample code and description for recovering archived objects using the COS C++ SDK.

Must-Knows

If you want to perform a restore operation on an archived object, you need to have the restore permission for that object. When you configure the authorization policy, the action needs to be set to cos:PostObjectRestore. For more authorization, see CAM-supported business APIs.

Related Examples

Function Name
Description
Example code
Recovering Archived Objects
An object of ARCHIVE or DEEP_ARCHIVE storage type is restored (thawed) to read its content.

Preliminary Preparation

Create CosAPI

Before calling the COS API, you must first create an instance of CosAPI to make subsequent call requests.
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// Region of the bucket, see https://www.tencentcloud.com/document/product/436/62?from_cn_redirect=1
std::string secret_id = "************************************"; // User's SecretId. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
std::string secret_key = "************************************"; // User's SecretKey. It is recommended to use sub-account keys, with authorization following the least privilege principle to mitigate usage risks. For information on how to obtain sub-account keys, see https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Create CosAPI Using a Temporary Key

To access COS with a temporary key, you need to create a CosAPI instance using the temporary key.
qcloud_cos::CosAPI InitCosAPI() {
// You need to have obtained the temporary key results: tmp_secret_id, tmp_secret_key,
// For generating temporary keys, see https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

Use Cases

Recovering Archived Objects

Method Prototype

CosResult CosAPI::PostObjectRestore(const PostObjectRestoreReq& req, PostObjectRestoreResp* resp)

Request Example

void PostObjectRestoreDemo(qcloud_cos::CosAPI& cos) {
std::string object_name = "test.txt";
qcloud_cos::PostObjectRestoreReq req(bucket_name, object_name); // A third parameter, version_id, can also be passed in.
req.SetExiryDays(1);// Sets the number of days for temporary restoration
qcloud_cos::PostObjectRestoreResp resp;

qcloud_cos::CosResult result = cos.PostObjectRestore(req, &resp);

std::cout << "===================PostObjectRestore=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
}

Parameter Description

Parameter Name
Description
Type
req
Recovering Archived Objects Request
PostObjectRestoreResp
resp
Restore Archived Objects Response
PostObjectRestoreResp
PostObjectRestoreResp Members and Functions Description:
Member or Function
Description
Parameter Type
bucket_name
Bucket name, which can be set via the constructor or set method.
The naming format for buckets is BucketName-APPID. For details, see Naming Conventions
string
object_name
Object key (Key), which can be set via the constructor or set method.
is the unique identifier of the object in the bucket. For example, in the object access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the object key is doc/picture.jpg. For details, see Object Key
string
version_id
Recovering object versions, which can be set via the constructor or set method.
When versioning is enabled, users should specify the version ID to restore; if no version ID is specified, the latest version of the object is restored.
string
SetTier
Set the specific restore process type. If not set, Standard is used.
If restoring ARCHIVE storage type data, you can call the setTier function to set it to one of the three restore modes: Standard, Expedited, or Bulk.
If restoring DEEP_ARCHIVE storage type, only two restore modes are supported: Standard and Bulk.
For the specific meaning of each mode, see Restore Mode.
string
SetExiryDays
Set the expiration days for the restored temporary files.
uint64_t
PostObjectRestoreResp Member Functions Description:
Member functions
Description
Return Type
GetEtag
Obtain the Etag of the stored object
string
GetXCosRequestId
Obtain the request ID
string

Returning Description

CosResult main member functions are as follows:
Member functions
Description
Return Type
IsSucc
Indicates whether the operation is successful; returns true for success, false for failure.
bool
GetHttpStatus
Obtain the http status code.
int
GetErrorCode
The error code can be obtained when the request fails.
string
GetErrorMsg
Obtain the error message when the request fails.
string
GetXCosRequestId
Obtain the request ID.
string
Usage examples for CosResult are as follows. Users may choose to utilize them based on their needs:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

API Operations

For API specifications related to restoring archived objects, see the POST Object restore documentation.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback