简体   繁体   English

在我的S3存储桶将不会在浏览器中打开图片,他们只会下载

[英]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应用程序的图像,我遇到的问题是,当我单击图像的链接时,它将下载该图像而不是在浏览器中显示该图像。

  1. I have set the metadata to be 'Content-Type: image/*' 我已将元数据设置为“内容类型:图片/ *”
  2. I have set the permissions to be public 我已将权限设置为公开

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.

相关问题 使用Java在Amazon S3中下载存储桶的密钥 - Key for download bucket in Amazon S3 using Java 如何下载S3存储桶上的整个文件夹? - How to Download entire Folder located on S3 Bucket? 从S3存储桶下载压缩的.gz文件中的文件 - Download the files inside compressed .gz files from S3 Bucket 如何在AWS S3存储桶中检索上载图像的URL - How to retrieve URL for uploaded images in AWS S3 bucket 上传的文件在我的项目和 AWS S3 存储桶中重复 - Uploaded files duplicated in my project and AWS S3 bucket 如何使用Java从Amazon S3存储桶中使用其名称下载垂直文件 - how to download a perticular file using it's name from amazon s3 bucket using java Amazon s3 只为一个桶返回 1000 个条目,而为另一个桶返回所有条目(使用 java sdk)? - Amazon s3 returns only 1000 entries for one bucket and all for another bucket (using java sdk)? Spring Boot Amazon AWS S3 存储桶文件下载 - 拒绝访问 - Spring Boot Amazon AWS S3 Bucket File Download - Access Denied 我们如何使用 Java SDK 从 S3 存储桶下载没有文件夹的多个文件 - How can we download multiple files without folder from S3 bucket using Java SDK 如何从S3存储桶的各个文件夹中下载多个与文件名具有相同前缀的文件? - How to download multiple files having same prefix as filename from various folders of S3 bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM