我目前使用以下方法将文件上传到 S3:
File file = new File(my_file_path);
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(cred));
s3.putObject(new PutObjectRequest("folder", key, file));
上面的工作正常,但我想直接保存BufferedImage
到 S3 来缩短我的应用程序的时间,但我不知道如何做到这一点?这就是我目前将图像保存到文件的方式:
image = new BufferedImage(rawImage.width, rawImage.height, BufferedImage.TYPE_INT_ARGB);
File file = new File(filepath);
ImageIO.write(image, "png", file);
有没有办法以流的形式直接写入 Amazon S3,如果可以,有人可以举个例子吗?
另外,这是一个好主意吗?如果它容易出错,我会坚持使用当前的方法。任何建议表示赞赏。
以下(或非常类似的内容)应该可以正常工作。避免写入物理文件的步骤应该比处理磁盘 I/O 更不容易出错(至少,随着时间的推移,磁盘被填满的机会会减少)。
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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)