Object Storage Service (OSS) は、ホットデータからコールドデータまでのさまざまなデータストレージシナリオをカバーするために、標準、低頻度アクセス (IA) 、アーカイブ、コールドアーカイブ、およびディープコールドアーカイブのストレージクラスを提供します。 OSSでは、オブジェクトが作成されると、そのコンテンツを変更することはできません。 オブジェクトのストレージクラスを変換する場合は、Bucket.CopyObjectメソッドを使用してオブジェクトをコピーし、新しいオブジェクトを作成し、新しいオブジェクトのストレージクラスを変換する必要があります。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
オブジェクトのストレージクラスを変換するには、
oss:GetObject
、oss:PutObject
、およびoss:RestoreObject
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。
例
オブジェクトのストレージクラスをStandardまたはIAからArchiveに変換
#include <iostream>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources, such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Specify the storage class to which you want to convert the object. In this example, the storage class is set 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);
/* Convert the storage class of the object to the specified storage class. */
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 resources, such as network resources. */
ShutdownSdk();
return 0;
}
オブジェクトのストレージクラスをアーカイブからIAまたは標準に変換
#include <iostream>
#include <algorithm>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources, such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Check whether the storage class of the object for which you want to convert the storage class is Archive. If the object is an Archive object, you must restore the object before you can convert the 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;
/* Wait until 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--;
}
std::string SourceBucketName = BucketName;
std::string SourceObjectName = ObjectName;
ObjectMetaData objectMeta;
/* Specify the storage class to which you want to convert the object. In this example, set the storage class to IA. */
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;
}
関連ドキュメント
オブジェクトのストレージクラスの変換に使用される完全なサンプルコードについては、GitHubをご覧ください。
オブジェクトのストレージクラスを変換するために呼び出すことができるAPI操作の詳細については、「CopyObject」をご参照ください。