Chadillac
Chadillac

Reputation: 429

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?

Upvotes: 2

Views: 10310

Answers (1)

CodeHunter
CodeHunter

Reputation: 2082

Change your Content-Type 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.

Upvotes: 6

Related Questions