File tree Expand file tree Collapse file tree
aws/src/main/java/com/baeldung/s3 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .baeldung .s3 ;
22
33import java .io .File ;
4- import java .io .FileOutputStream ;
54import java .io .IOException ;
65
6+ import org .apache .commons .io .FileUtils ;
7+
78import com .amazonaws .auth .AWSCredentials ;
89import com .amazonaws .auth .AWSStaticCredentialsProvider ;
910import 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 (
You can’t perform that action at this time.
0 commit comments