如何写一个S3对象到一个文件?如何写、对象、文件

2023-09-11 09:31:29 作者:森绿

什么是写一个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.

谢谢 杰森

推荐答案

IOUtils.copy() IOUtils.copyLarge()是伟大的,我想preFER通过的InputStream,直到输入流返回-1循环的老学校的方式。为什么?我用IOUtils.copy()之前,但有一个特定的使用情况下,如果我开始下载一个大文件从S3,然后由于某种原因,如果线程被中断,下载将不会停止,它会继续下去,直到整个文件被下载。

While IOUtils.copy() and IOUtils.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.

当然,这已经无关S3,只是IOUtils库。

Of course, this has nothing to do with S3, just the IOUtils library.

所以,我preFER这样的:

So, I prefer this:

InputStream in = s3Object.getObjectContent();
byte[] buf = new byte[1024];
OutputStream out = new FileOutputStream(file);
while( (count = in.read(buf)) != -1)
{
   if( Thread.interrupted() )
   {
       throw new InterruptedException();
   }
   out.write(buf, 0, count);
}
out.close();
in.close();

请注意:这也意味着你不需要额外的库

Note: This also means you don't need additional libraries

 
精彩推荐
图片推荐