简体   繁体   English

如何为 S3 对象创建 java OutputStream 并向其写入值?

[英]How to create a java OutputStream for an S3 object and write value to it?

Existing ways of adding content to an S3 file using methods in AmazonS3 class are使用 AmazonS3 类中的方法将内容添加到 S3 文件的现有方法是

  • by putObject with an InputStream通过 putObject 和 InputStream
  • Creating a local file with content and uploading it to S3.创建包含内容的本地文件并将其上传到 S3。

Is there a way an OutputStream can be created for an existing S3 object to which values from a list can be written into?有没有一种方法可以为现有的 S3 对象创建一个可以写入列表中的值的输出流? I see there are no APIs for doing so.我看到没有 API 可以这样做。

It's possible to create an S3OutputStream which wraps the AmazonS3 client.可以创建一个包装 AmazonS3 客户端的 S3OutputStream。 See this gist for the implementation: https://gist.github.com/blagerweij/ad1dbb7ee2fff8bcffd372815ad310eb It automatically detects large files, and uses multi-part transfers when required.有关实现,请参阅此要点: https ://gist.github.com/blagerweij/ad1dbb7ee2fff8bcffd372815ad310eb 它会自动检测大文件,并在需要时使用多部分传输。 It uses a byte-array for buffering, the size of that buffer depends on your use case.它使用字节数组进行缓冲,该缓冲区的大小取决于您的用例。 (default is 10MB) (默认为 10MB)

For example:例如:

   final S3Client s3Client = AmazonS3ClientBuilder.defaultClient();
   final OutputStream out = new S3OutputStream(s3Client, "bucket", "path/file.ext");

You can take a look at AWS SDK Performing Operations on an Amazon S3 Object您可以查看 AWS SDK Performing Operations on an Amazon S3 对象

...
S3Client s3;
...
// Put Object
s3.putObject(bucket,object);

blagerweij, is there a code for aws sdk 2+? blagerweij,是否有适用于 aws sdk 2+ 的代码? Your sample is using S3Client, but code used 1.* version.您的示例使用的是 S3Client,但代码使用的是 1.* 版本。

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM