如何将 S3 对象写入文件?
将 S3 对象(我有密钥)写入文件的最快方法是什么?我正在使用Java。
What's the fastest way to write an S3 object (of which I have the key) to a file? I'm using Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
自Java 7(于 2011 年 7 月发布)以来,有更好的方法:
Files.copy()
实用程序来自java.util.nio.file
。因此,您既不需要外部库,也不需要自己的字节数组循环。下面的两个示例均使用来自
S3Object.getObjectContent()
的输入流。1) 写入指定路径的新文件:
2) 写入系统默认 tmp 位置的临时文件:(
如果不指定替换现有文件的选项,您将将得到一个
FileAlreadyExistsException
。)另请注意
getObjectContent( )
Javadocs 敦促您关闭输入流:因此,将所有内容包装在 try-catch-finally 中,并在 finally 块中执行
in.close();
应该是最安全的。以上假设您使用 Amazon 的官方 SDK (
aws-java-sdk-s3
)。Since Java 7 (published back in July 2011), there’s a better way:
Files.copy()
utility fromjava.util.nio.file
.So you need neither an external library nor rolling your own byte array loops. Two examples below, both of which use the input stream from
S3Object.getObjectContent()
.1) Write to a new file at specified path:
2) Write to a temp file in system's default tmp location:
(Without specifying the option to replace existing file, you'll get a
FileAlreadyExistsException
.)Also note that
getObjectContent()
Javadocs urge you to close the input stream: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 (
aws-java-sdk-s3
).虽然 IOUtils.copy() 和 IOUtils.copyLarge() 很棒,但我更喜欢循环输入流直到输入流返回 -1 的老式方法。为什么?我之前使用过 IOUtils.copy(),但有一个特定的用例,如果我开始从 S3 下载一个大文件,然后由于某种原因,如果该线程被中断,下载将不会停止,并且会一直持续下去,直到整个文件已下载。
当然,这与S3无关,只是IOUtils库。
所以,我更喜欢这个:
注意:这也意味着你不需要额外的库
While
IOUtils.copy()
andIOUtils.copyLarge()
are great, I would prefer the old school way of looping through the inputstream until the inputstream returns -1. Why? I used IOUtils.copy() before but there was a specific use case where if I started downloading a large file from S3 and then for some reason if that thread was interrupted, the download would not stop and it would go on and on until the whole file was downloaded.Of course, this has nothing to do with S3, just the IOUtils library.
So, I prefer this:
Note: This also means you don't need additional libraries
AmazonS3Client 类具有以下方法:
返回的 S3Object 具有方法...
..以流形式获取对象内容。我会使用 Apache Commons 中的 IOUtils,如下所示:
IOUtils.copy(s3Object.getObjectContent(), new FileOutputStream(new File(filepath)));
The AmazonS3Client class has the following method:
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)));
使用 TransferManager 的这个班轮怎么样:
What about this one liner using a TransferManager:
从 2017 年发布的 AWS SDK for Java v2 开始,您只需指定用于写入文件的
Path
即可。https: //docs.aws.amazon.com/sdk-for-java/v2/developer-guide/examples-s3-objects.html#download-object
如果您需要
文件
,你可以使用toFile
方法。From AWS SDK for Java v2 released in 2017, you can just specify a
Path
for writing to the file.https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/examples-s3-objects.html#download-object
If you need a
File
, you can usetoFile
method.