Utilisation RestoreObject avec un AWS SDK ou une CLI - Amazon Simple Storage Service

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utilisation RestoreObject avec un AWS SDK ou une CLI

Les exemples de code suivants montrent comment utiliserRestoreObject.

.NET
AWS SDK for .NET
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code 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}"); } }
  • Pour plus de détails sur l'API, voir RestoreObjectla section Référence des AWS SDK for .NET API.

CLI
AWS CLI

Pour créer une demande de restauration pour un objet

L'restore-objectexemple suivant restaure l'objet Amazon S3 Glacier spécifié pour le compartiment my-glacier-bucket pendant 10 jours.

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

Cette commande ne produit aucun résultat.

  • Pour plus de détails sur l'API, reportez-vous RestoreObjectà la section Référence des AWS CLI commandes.

Java
SDK pour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code 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); } } }
  • Pour plus de détails sur l'API, voir RestoreObjectla section Référence des AWS SDK for Java 2.x API.

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de ce service avec un AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.