如何将 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.
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
并接受额外线程的开销(请参阅 这个答案)
- 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
是 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屋!