Skip to content

Commit eab110d

Browse files
ramansahasimaibin
authored andcommitted
BAEL-787 (updated) - AWS S3 with Java (eugenp#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
1 parent 318d079 commit eab110d

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

aws/src/main/java/com/baeldung/s3/S3Application.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.baeldung.s3;
22

33
import java.io.File;
4-
import java.io.FileOutputStream;
54
import java.io.IOException;
65

6+
import org.apache.commons.io.FileUtils;
7+
78
import com.amazonaws.auth.AWSCredentials;
89
import com.amazonaws.auth.AWSStaticCredentialsProvider;
910
import com.amazonaws.auth.BasicAWSCredentials;
@@ -28,7 +29,7 @@ public class S3Application {
2829
"<AWS accesskey>",
2930
"<AWS secretkey>"
3031
);
31-
}
32+
}
3233

3334
public static void main(String[] args) throws IOException {
3435
//set-up the client
@@ -74,15 +75,7 @@ public static void main(String[] args) throws IOException {
7475
//downloading an object
7576
S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
7677
S3ObjectInputStream inputStream = s3object.getObjectContent();
77-
FileOutputStream fos = new FileOutputStream(new File("/Users/user/Desktop/hello.txt"));
78-
79-
int read = 0;
80-
byte[] bytes = new byte[1024];
81-
while ((read = inputStream.read(bytes)) != -1) {
82-
fos.write(bytes, 0, read);
83-
}
84-
inputStream.close();
85-
fos.close();
78+
FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt"));
8679

8780
//copying an object
8881
awsService.copyObject(

0 commit comments

Comments
 (0)