Objects in the Archive and ColdArchive storage classes must be restored before they can be read. The OSS C++ SDK V2 provides the RestoreObject operation to restore archived objects and the CleanRestoredObject operation to revert them to the archived state.
Usage notes
-
Before you run the sample code, replace
<region>with the ID of your region, such ascn-hangzhou. -
The sample code uses environment variables to obtain access credentials.
-
Restoring an archive file requires the
oss:RestoreObjectpermission. -
RestoreObject applies only to objects in the Archive and ColdArchive storage classes.
It takes about one minute to restore an object from the Archive storage class. For objects in the ColdArchive storage class, the retrieval time varies based on the data size and retrieval priority. After an object is restored, it becomes readable.
Sample code
Restore an archive file
The following code restores an archive file:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
oss::models::RestoreRequest restoreReq;
restoreReq.days = 1;
restoreReq.jobParameters = oss::models::JobParameters().setTier("Standard");
auto outcome = client.restoreObject(
oss::models::RestoreObjectRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt")
.setRestoreRequest(restoreReq));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to restore the archive file"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Successfully restored the archive file"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId() << std::endl;
return 0;
}
Clear the restoration status
The following code clears the restoration status of a restored object and returns it to the archived state:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.cleanRestoredObject(
oss::models::CleanRestoredObjectRequest()
.setBucket("examplebucket")
.setKey("exampleobject.txt"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to clear the restoration status"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Successfully cleared the restoration status"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId() << std::endl;
return 0;
}
Related documents
-
For the complete sample code for restoring an archive file, see RestoreObject.cpp.
-
For the complete sample code for clearing the restoration status, see CleanRestoredObject.cpp.