简体   繁体   中英

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?

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.

  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. You need to set the appropriate content type as set in your S3 bucket. For example, if it is a text, set it as text/plain . Similarly for image, set it to image/png . Also, your file content type should not be binary/octet-stream , otherwise, it won't open up, rather it will download then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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