Object Storage Service (OSS) provides the following storage classes to cover various data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. In OSS, once an object is created, its content cannot be modified. If you want to convert the storage class of an object, you must use the Bucket.CopyObject method to copy the object to create a new object and convert the storage class of the new object.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To convert the storage class of an object, you must have the
oss:GetObject,oss:PutObject, andoss:RestoreObjectpermissions. For more information, see Attach a custom policy to a RAM user.
Sample code
Transform the storage class of an object from Standard or Infrequent Access to Archive
#include <iostream>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Set the storage class for the object. For example, set the storage class to Archive. */
ObjectMetaData objectMeta;
objectMeta.addHeader("x-oss-storage-class", "Archive");
std::string SourceBucketName = BucketName;
std::string SourceObjectName = ObjectName;
CopyObjectRequest request(SourceBucketName, ObjectName, objectMeta);
request.setCopySource(SourceBucketName, SourceObjectName);
/* Change the storage class to the specified one. */
auto outcome = client.CopyObject(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "CopyObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Transform the storage class of an object from Archive to Infrequent Access or Standard
#include <iostream>
#include <algorithm>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Check whether the object is an archived object. If it is, you must restore it before you can change its storage class. */
auto restoreOutcome = client.RestoreObject(BucketName, ObjectName);
if (!restoreOutcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "RestoreObject fail" <<
",code:" << restoreOutcome.error().Code() <<
",message:" << restoreOutcome.error().Message() <<
",requestId:" << restoreOutcome.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 archived object is successfully restored. */
break;
}
std::cout << " info, WaitTime:" << maxWaitTimeInSeconds
<< "; restore status:" << restoreStatus << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
maxWaitTimeInSeconds--;
}
std::string SourceBucketName = BucketName;
std::string SourceObjectName = ObjectName;
ObjectMetaData objectMeta;
/* Set the storage class for the object. For example, set the storage class to Infrequent Access. */
objectMeta.addHeader("x-oss-storage-class", "IA");
CopyObjectRequest request(SourceBucketName, SourceObjectName, objectMeta);
request.setCopySource(SourceBucketName, SourceObjectName);
auto outcome = client.CopyObject(request);
if (!outcome.isSuccess()) {
std::cout << "CopyObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
ShutdownSdk();
return 0;
}
References
For the complete sample code that shows how to transform the storage class of an object, see GitHub example.
For more information about the API operation that you can use to transform the storage class of an object, see CopyObject.