Path: blob/master/code_examples/java_examples/S3Examples/RestoreArchivedObject.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.model.ObjectMetadata;11import com.amazonaws.services.s3.model.RestoreObjectRequest;1213public class RestoreArchivedObject {1415public static void main(String[] args) throws IOException {16String clientRegion = "*** Client region ***";17String bucketName = "*** Bucket name ***";18String keyName = "*** Object key ***";1920try {21AmazonS3 s3Client = AmazonS3ClientBuilder.standard()22.withCredentials(new ProfileCredentialsProvider())23.withRegion(clientRegion)24.build();2526// Create and submit a request to restore an object from Glacier for two days.27RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, keyName, 2);28s3Client.restoreObjectV2(requestRestore);2930// Check the restoration status of the object.31ObjectMetadata response = s3Client.getObjectMetadata(bucketName, keyName);32Boolean restoreFlag = response.getOngoingRestore();33System.out.format("Restoration status: %s.\n",34restoreFlag ? "in progress" : "not in progress (finished or failed)");35}36catch(AmazonServiceException e) {37// The call was transmitted successfully, but Amazon S3 couldn't process38// it, so it returned an error response.39e.printStackTrace();40}41catch(SdkClientException e) {42// Amazon S3 couldn't be contacted for a response, or the client43// couldn't parse the response from Amazon S3.44e.printStackTrace();45}46}47}4849