GetS3Object.java Source code

Java tutorial

Introduction

Here is the source code for GetS3Object.java

Source

/*
   Copyright 2010-2017 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();
    }
}