forked from awsdocs/aws-java-developer-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetS3Object.java
More file actions
58 lines (48 loc) · 1.81 KB
/
GetS3Object.java
File metadata and controls
58 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
import java.io.*;
import com.amazonaws.auth.*;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
public class GetS3Object {
private static String bucketName = "text-content";
private static String key = "text-object.txt";
public static void main(String[] args) throws IOException
{
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
try {
System.out.println("Downloading an object");
S3Object s3object = s3Client.getObject(
new GetObjectRequest(bucketName, key));
displayTextInputStream(s3object.getObjectContent());
}
catch(AmazonServiceException ase) {
System.err.println("Exception was thrown by the service");
}
catch(AmazonClientException ace) {
System.err.println("Exception was thrown by the client");
}
}
private static void displayTextInputStream(InputStream input) throws IOException
{
// Read one text line at a time and display.
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while(true)
{
String line = reader.readLine();
if(line == null) break;
System.out.println( " " + line );
}
System.out.println();
}
}