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
This commit is contained in:
ramansahasi 2017-07-20 21:36:54 +05:30 committed by maibin
parent 318d0790f5
commit eab110d0d0
1 changed files with 4 additions and 11 deletions

View File

@ -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 {
"<AWS accesskey>",
"<AWS secretkey>"
);
}
}
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(