AWS S3 Java SDK - Download file help AWS S3 Java SDK - Download file help java java

AWS S3 Java SDK - Download file help


Though the code written in Mauricio's answer will work - and his point about streams is of course correct - Amazon offers a quicker way to save files in their SDK. I don't know if it wasn't available in 2011 or not, but it is now.

AmazonS3Client s3Client = new AmazonS3Client(myCredentials);File localFile = new File("localFilename");ObjectMetadata object = s3Client.getObject(new GetObjectRequest("bucket", "s3FileName"), localFile);


Instead of Reader and Writer classes you should be using InputStream and OutputStream classes:

InputStream reader = new BufferedInputStream(   object.getObjectContent());File file = new File("localFilename");      OutputStream writer = new BufferedOutputStream(new FileOutputStream(file));int read = -1;while ( ( read = reader.read() ) != -1 ) {    writer.write(read);}writer.flush();writer.close();reader.close();


Eyals answer gets you half way there but its not all that clear so I will clarify.

AmazonS3Client s3Client = new AmazonS3Client(myCredentials);//This is where the downloaded file will be savedFile localFile = new File("localFilename");//This returns an ObjectMetadata file but you don't have to use this if you don't want s3Client.getObject(new GetObjectRequest(bucketName, id.getId()), localFile);//Now your file will have your image saved boolean success = localFile.exists() && localFile.canRead();