如何将Java OutputStream上载到AWS S3 [英] How to upload a Java OutputStream to AWS S3

查看:580
本文介绍了如何将Java OutputStream上载到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 OutputStreams. 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:


  1. OutputStream 复制到 InputStream ,并接受使用的RAM量的两倍。

  2. OutputStream 传递给 InputStream 并接受额外线程的开销(参见< a href =https://stackoverflow.com/questions/1225909/most-efficient-way-to-create-inputstream-from-outputstream>这个答案)

  1. Copy the OutputStream to an InputStream and accept that twice the amount of RAM is used.
  2. Pipe the OutputStream to an InputStream 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屋!

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