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:
parent
318d0790f5
commit
eab110d0d0
@ -1,9 +1,10 @@
|
|||||||
package com.baeldung.s3;
|
package com.baeldung.s3;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
import com.amazonaws.auth.AWSCredentials;
|
import com.amazonaws.auth.AWSCredentials;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
@ -28,7 +29,7 @@ public class S3Application {
|
|||||||
"<AWS accesskey>",
|
"<AWS accesskey>",
|
||||||
"<AWS secretkey>"
|
"<AWS secretkey>"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
//set-up the client
|
//set-up the client
|
||||||
@ -74,15 +75,7 @@ public class S3Application {
|
|||||||
//downloading an object
|
//downloading an object
|
||||||
S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
|
S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
|
||||||
S3ObjectInputStream inputStream = s3object.getObjectContent();
|
S3ObjectInputStream inputStream = s3object.getObjectContent();
|
||||||
FileOutputStream fos = new FileOutputStream(new File("/Users/user/Desktop/hello.txt"));
|
FileUtils.copyInputStreamToFile(inputStream, 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();
|
|
||||||
|
|
||||||
//copying an object
|
//copying an object
|
||||||
awsService.copyObject(
|
awsService.copyObject(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user