Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Restore an Archived Object Using the AWS SDK for Java

The following example shows how to restore an object archived to Amazon Glacier using the AWS SDK for Java. The example initiates a restoration request for the specified archived object and checks its restoration status. For more information about restoring archived objects, see Restoring Archived Objects. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.

// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.) import java.io.IOException; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.RestoreObjectRequest; public class RestoreArchivedObject { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Object key ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Create and submit a request to restore an object from Glacier for two days. RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, keyName, 2); s3Client.restoreObjectV2(requestRestore); // Check the restoration status of the object. ObjectMetadata response = s3Client.getObjectMetadata(bucketName, keyName); Boolean restoreFlag = response.getOngoingRestore(); System.out.format("Restoration status: %s.\n", restoreFlag ? "in progress" : "not in progress (finished or failed)"); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }