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

查看:78
本文介绍了如何将 Java OutputStream 上传到 AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内存中创建 PDF 文档作为 OutputStreams.这些应该上传到 S3.我的问题是无法直接从 OutputStream 创建一个 PutObjectRequest(根据 AWS 开发论坛中的此主题).我在 Dropwizard 应用中使用了 aws-java-sdk-s3 v1.10.8.

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 并接受额外线程的开销(请参阅 这个答案)
  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?

我的 OutputStreamByteArrayOutputStreams

推荐答案

我通过子类化 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天全站免登陆