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