归档或冷归档类型的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。