Gunakan RestoreObject dengan AWS SDK atau CLI - Amazon Simple Storage Service

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan RestoreObject dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanRestoreObject.

.NET
AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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}"); } }
  • Untuk detail API, lihat RestoreObjectdi Referensi AWS SDK for .NET API.

CLI
AWS CLI

Untuk membuat permintaan pemulihan untuk objek

restore-objectContoh berikut mengembalikan objek Amazon S3 Glacier yang ditentukan untuk bucket selama 10 hari. my-glacier-bucket

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

Perintah ini tidak menghasilkan output.

  • Untuk detail API, lihat RestoreObjectdi Referensi AWS CLI Perintah.

Java
SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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); } } }
  • Untuk detail API, lihat RestoreObjectdi Referensi AWS SDK for Java 2.x API.

Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMenggunakan layanan ini dengan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.