[英]Images in my s3 bucket will not open in the browser, they will only download
How do I open an image on my s3 bucket in the browser? 如何在浏览器的s3存储桶中打开图像?
I have setup an Amazon s3 bucket to host my webapp's images, the issue I am having is that when I click on the image's link, it will download the image instead of displaying it in the browser. 我已经设置了一个Amazon s3存储桶来托管我的Web应用程序的图像,我遇到的问题是,当我单击图像的链接时,它将下载该图像而不是在浏览器中显示该图像。
This is the code I am using: 这是我正在使用的代码:
private void uploadFileTos3bucket(String fileName, MultipartFile file)
{
try
(InputStream is = file.getInputStream())
{ ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getBytes().length);
metadata.setLastModified(new Date());
metadata.setContentType("image/*"); s3client.putObject(new
PutObjectRequest(bucketName, fileName, is, metadata).withCannedAcl(CannedAccessControlList.PublicRead)); }
catch(IOException e) { e.printStackTrace();
}
}
Does anyone have any other ideas or solutions? 有人还有其他想法或解决方案吗?
Change your contenttype
to image/png
, considering that your image in S3 is in .png
format. 考虑到S3中的图像为.png
格式,请将您的contenttype
更改为image/png
。 You need to set the appropriate content type as set in your S3 bucket. 您需要按照S3存储桶中的设置来设置适当的内容类型。 For example, if it is a text, set it as text/plain
. 例如,如果它是文本,则将其设置为text/plain
。 Similarly for image, set it to image/png
. 对于图像类似,将其设置为image/png
。 Also, your file content type should not be binary/octet-stream
, otherwise, it won't open up, rather it will download then. 另外,您的文件内容类型不应为binary/octet-stream
,否则将不会打开,而是先下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.