What's the fastest way to write an S3 object (of which I have the key) to a file? I'm using Java.
How to write an S3 object to a file?
The AmazonS3Client class has the following method:
S3Object getObject(String bucketName, String key)
The returned S3Object has the method...
..which gets the object content as a stream. I'd use IOUtils from Apache Commons like this:
IOUtils.copy(s3Object.getObjectContent(), new FileOutputStream(new File(filepath)));
Copies all bytes from an input stream to a file.
InputStream in = s3Client.getObject("bucketName", "key").getObjectContent();
1) Write to a new file at specified path:
Files.copy(in, new File("/my/path/file.jpg").toPath());
2) Write to a temp file in system's default tmp location:
File tmp = File.createTempFile("s3test", ""); Files.copy(in, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
(Without specifying the option to replace existing file, you'll get a
Also note that
getObjectContent() Javadocs urge you to close the input stream:
If you retrieve an S3Object, you should close this input stream as soon as possible, because the object contents aren't buffered in memory and stream directly from Amazon S3. Further, failure to close this stream can cause the request pool to become blocked.
So it should be safest to wrap everything in try-catch-finally, and do
in.close(); in the finally block.
The above assumes that you use the official SDK from Amazon (