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