Restore an Archived Object Using the AWS SDK for Java

Restore an Archived Object Using the AWS SDK for Java

Example
The following example shows how to restore an object archived to Amazon S3 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.

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
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;
import java.io.IOException;
public class RestoreArchivedObject {
public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.DEFAULT_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();
}
}
}