AWS SDK を使用して、オブジェクトのアーカイブされたコピーを Amazon S3 バケットに復元する - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して、オブジェクトのアーカイブされたコピーを Amazon S3 バケットに復元する

次のコード例は、オブジェクトのアーカイブされたコピーを S3 バケットに復元する方法を示しています。

.NET
AWS SDK for .NET
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to restore an archived object in an Amazon /// Simple Storage Service (Amazon S3) bucket. /// </summary> public class RestoreArchivedObject { public static void Main() { string bucketName = "doc-example-bucket"; string objectKey = "archived-object.txt"; // Specify your bucket region (an example region is shown). RegionEndpoint bucketRegion = RegionEndpoint.USWest2; IAmazonS3 client = new AmazonS3Client(bucketRegion); RestoreObjectAsync(client, bucketName, objectKey).Wait(); } /// <summary> /// This method restores an archived object from an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// RestoreObjectAsync.</param> /// <param name="bucketName">A string representing the name of the /// bucket where the object was located before it was archived.</param> /// <param name="objectKey">A string representing the name of the /// archived object to restore.</param> public static async Task RestoreObjectAsync(IAmazonS3 client, string bucketName, string objectKey) { try { var restoreRequest = new RestoreObjectRequest { BucketName = bucketName, Key = objectKey, Days = 2, }; RestoreObjectResponse response = await client.RestoreObjectAsync(restoreRequest); // Check the status of the restoration. await CheckRestorationStatusAsync(client, bucketName, objectKey); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine($"Error: {amazonS3Exception.Message}"); } } /// <summary> /// This method retrieves the status of the object's restoration. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// GetObjectMetadataAsync.</param> /// <param name="bucketName">A string representing the name of the Amazon /// S3 bucket which contains the archived object.</param> /// <param name="objectKey">A string representing the name of the /// archived object you want to restore.</param> public static async Task CheckRestorationStatusAsync(IAmazonS3 client, string bucketName, string objectKey) { GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest() { BucketName = bucketName, Key = objectKey, }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); var restStatus = response.RestoreInProgress ? "in-progress" : "finished or failed"; Console.WriteLine($"Restoration status: {restStatus}"); } }
  • API の詳細については、「 API リファレンスRestoreObject」の「」を参照してください。 AWS SDK for .NET

CLI
AWS CLI

オブジェクトの復元リクエストを作成するには

次の restore-object の例では、指定された Amazon S3 Glacier オブジェクトをバケット my-glacier-bucket に10 日間復元します。

aws s3api restore-object \ --bucket my-glacier-bucket \ --key doc1.rtf \ --restore-request Days=10

このコマンドでは何も出力されません。

  • API の詳細については、「 コマンドリファレンスRestoreObject」の「」を参照してください。 AWS CLI

Java
SDK for Java 2.x
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.RestoreRequest; import software.amazon.awssdk.services.s3.model.GlacierJobParameters; import software.amazon.awssdk.services.s3.model.RestoreObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.Tier; /* * For more information about restoring an object, see "Restoring an archived object" at * https://docs.aws.amazon.com/AmazonS3/latest/userguide/restoring-objects.html * * Before running this Java V2 code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RestoreObject { public static void main(String[] args) { final String usage = """ Usage: <bucketName> <keyName> <expectedBucketOwner> Where: bucketName - The Amazon S3 bucket name.\s keyName - The key name of an object with a Storage class value of Glacier.\s expectedBucketOwner - The account that owns the bucket (you can obtain this value from the AWS Management Console).\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; String expectedBucketOwner = args[2]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); restoreS3Object(s3, bucketName, keyName, expectedBucketOwner); s3.close(); } public static void restoreS3Object(S3Client s3, String bucketName, String keyName, String expectedBucketOwner) { try { RestoreRequest restoreRequest = RestoreRequest.builder() .days(10) .glacierJobParameters(GlacierJobParameters.builder().tier(Tier.STANDARD).build()) .build(); RestoreObjectRequest objectRequest = RestoreObjectRequest.builder() .expectedBucketOwner(expectedBucketOwner) .bucket(bucketName) .key(keyName) .restoreRequest(restoreRequest) .build(); s3.restoreObject(objectRequest); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • API の詳細については、「 API リファレンスRestoreObject」の「」を参照してください。 AWS SDK for Java 2.x