将BufferedImage对象作为文件保存到Amazon S3 [英] Save a BufferedImage object to Amazon S3 as a file

查看:104
本文介绍了将BufferedImage对象作为文件保存到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下内容将文件上传到S3:

I currently make use of the following to upload a file to S3:

File file = new File(my_file_path);

AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(cred));

s3.putObject(new PutObjectRequest("folder", key, file));

以上工作正常,但我想直接保存 BufferedImage 到S3从我的应用程序中刮几秒钟,但我不知道如何做到这一点?这就是我目前将图像保存到文件的方式:

The above works fine but I want to directly save a BufferedImage to S3 to shave a few seconds from my application but I have no clue on how to do this? This is how I currently save my image to a file:

image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);

File file = new File(filepath); 

ImageIO.write(image, "png", file);

有没有办法直接写入Amazon S3作为流,如果有的话,可以有人举个例子?

Is there a way to do the writing directly to Amazon S3 as a stream, if so, can someone show an example?

另外,这是个好主意吗?如果它容易出错,我会坚持我现在的方法。任何建议表示赞赏。

In addition, is this a good idea? If its prone to errors, I'll stick to my current method. Any advice appreciated.

推荐答案

以下(或类似的东西)应该可以正常工作。避免写入物理文件的步骤应该比处理磁盘I / O稍微容易出错(至少,随着时间的推移填满磁盘的可能性会降低)。

The following (or something very similar) should work fine. Avoiding the step of writing to a physical file should be slightly less error-prone than dealing with disk I/O (at the very least, your chances of filling up your disk over time are reduced).

BufferedImage image = ...
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
byte[] buffer = os.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(cred));
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(buffer.length);
s3.putObject(new PutObjectRequest("folder", key, is, meta));

这篇关于将BufferedImage对象作为文件保存到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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