如何将Java OutputStream上载到AWS S3 [英] How to upload a Java OutputStream to AWS S3
问题描述
我在内存中创建PDF文档为 OutputStream
s。这些应该上传到S3。我的问题是,无法直接从 OutputStream
创建 PutObjectRequest
(根据 AWS开发论坛中的这个主题)。我在 Dropwizard中使用 aws-java-sdk-s3
v1.10.8 app。
I create PDF docs in memory as OutputStream
s. These should be uploaded to S3. My problem is that it's not possible to create a PutObjectRequest
from an OutputStream
directly (according to this thread in the AWS dev forum). I use aws-java-sdk-s3
v1.10.8 in a Dropwizard app.
到目前为止我能看到的两个解决方法是:
The two workarounds I can see so far are:
- 将
OutputStream
复制到InputStream
,并接受使用的RAM量的两倍。 - 将
OutputStream
传递给InputStream
并接受额外线程的开销(参见< a href =https://stackoverflow.com/questions/1225909/most-efficient-way-to-create-inputstream-from-outputstream>这个答案)
- Copy the
OutputStream
to anInputStream
and accept that twice the amount of RAM is used. - Pipe the
OutputStream
to anInputStream
and accept the overhead of an extra thread (see this answer)
如果我找不到更好的解决方案,我将选择#1,因为在我的设置中,它看起来好像我能比线程/ CPU更容易买得起额外的内存。
If i don't find a better solution I'll go with #1, because it looks as if I could afford the extra memory more easily than threads/CPU in my setup.
到目前为止,我是否还有其他可能更有效的方式来实现这一目标?
Is there any other, possibly more efficient way to achive this that I have overlooked so far?
修改:
我的 OutputStream
s是 ByteArrayOutputStream
s
推荐答案
我通过继承 ConvertibleOutputStream
解决了这个问题:
I solved this by subclassing ConvertibleOutputStream
:
public class ConvertibleOutputStream extends ByteArrayOutputStream {
//Craetes InputStream without actually copying the buffer and using up mem for that.
public InputStream toInputStream(){
return new ByteArrayInputStream(buf, 0, count);
}
}
这篇关于如何将Java OutputStream上载到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!