如何写一个S3对象到一个文件? [英] How to write an S3 object to a file?

查看:115
本文介绍了如何写一个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.

谢谢 杰森

推荐答案

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

这篇关于如何写一个S3对象到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆