From eab110d0d00d0d713acea2230d2de0715bebb6b6 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Thu, 20 Jul 2017 21:36:54 +0530 Subject: [PATCH] BAEL-787 (updated) - AWS S3 with Java (#2296) * Replacing manual handling of stream Replacing manual handling of stream apache commons FileUtils library. * Replacing manual handling of stream Replacing manual handling of stream apache commons FileUtils library. * Update S3Application.java --- .../main/java/com/baeldung/s3/S3Application.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/aws/src/main/java/com/baeldung/s3/S3Application.java b/aws/src/main/java/com/baeldung/s3/S3Application.java index fcbd7811b5..fdfb909f73 100644 --- a/aws/src/main/java/com/baeldung/s3/S3Application.java +++ b/aws/src/main/java/com/baeldung/s3/S3Application.java @@ -1,9 +1,10 @@ package com.baeldung.s3; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import org.apache.commons.io.FileUtils; + import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; @@ -28,7 +29,7 @@ public class S3Application { "", "" ); - } + } public static void main(String[] args) throws IOException { //set-up the client @@ -74,15 +75,7 @@ public class S3Application { //downloading an object S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt"); S3ObjectInputStream inputStream = s3object.getObjectContent(); - FileOutputStream fos = new FileOutputStream(new File("/Users/user/Desktop/hello.txt")); - - int read = 0; - byte[] bytes = new byte[1024]; - while ((read = inputStream.read(bytes)) != -1) { - fos.write(bytes, 0, read); - } - inputStream.close(); - fos.close(); + FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt")); //copying an object awsService.copyObject(