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/GetObject.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.BufferedReader;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.InputStreamReader;
8
9
import com.amazonaws.AmazonServiceException;
10
import com.amazonaws.SdkClientException;
11
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
12
import com.amazonaws.services.s3.AmazonS3;
13
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14
import com.amazonaws.services.s3.model.GetObjectRequest;
15
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
16
import com.amazonaws.services.s3.model.S3Object;
17
18
public class GetObject {
19
20
public static void main(String[] args) throws IOException {
21
String clientRegion = "*** Client region ***";
22
String bucketName = "*** Bucket name ***";
23
String key = "*** Object key ***";
24
25
S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
26
try {
27
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
28
.withRegion(clientRegion)
29
.withCredentials(new ProfileCredentialsProvider())
30
.build();
31
32
// Get an object and print its contents.
33
System.out.println("Downloading an object");
34
fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
35
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
36
System.out.println("Content: ");
37
displayTextInputStream(fullObject.getObjectContent());
38
39
// Get a range of bytes from an object and print the bytes.
40
GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key)
41
.withRange(0,9);
42
objectPortion = s3Client.getObject(rangeObjectRequest);
43
System.out.println("Printing bytes retrieved.");
44
displayTextInputStream(objectPortion.getObjectContent());
45
46
// Get an entire object, overriding the specified response headers, and print the object's content.
47
ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
48
.withCacheControl("No-cache")
49
.withContentDisposition("attachment; filename=example.txt");
50
GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName, key)
51
.withResponseHeaders(headerOverrides);
52
headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
53
displayTextInputStream(headerOverrideObject.getObjectContent());
54
}
55
catch(AmazonServiceException e) {
56
// The call was transmitted successfully, but Amazon S3 couldn't process
57
// it, so it returned an error response.
58
e.printStackTrace();
59
}
60
catch(SdkClientException e) {
61
// Amazon S3 couldn't be contacted for a response, or the client
62
// couldn't parse the response from Amazon S3.
63
e.printStackTrace();
64
}
65
finally {
66
// To ensure that the network connection doesn't remain open, close any open input streams.
67
if(fullObject != null) {
68
fullObject.close();
69
}
70
if(objectPortion != null) {
71
objectPortion.close();
72
}
73
if(headerOverrideObject != null) {
74
headerOverrideObject.close();
75
}
76
}
77
}
78
79
private static void displayTextInputStream(InputStream input) throws IOException {
80
// Read the text input stream one line at a time and display each line.
81
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
82
String line = null;
83
while ((line = reader.readLine()) != null) {
84
System.out.println(line);
85
}
86
System.out.println();
87
}
88
}
89