Harsha M V
Harsha M V

Reputation: 54949

AWS S3 Stop Download Image automatically when browsed via URL

I am uploading images to my S3 bucket via my Node.js Application. I have the following bucket policy.

{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicRead",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::bucket_name/*"
    }
  ]
}

When I go to the link of the file lets say its an image. It automatically downloads as a file and does not render in the browser. Any idea how to make it display in the browser without automatically force downloading the image in the browser?

Upvotes: 5

Views: 4173

Answers (1)

franklinsijo
franklinsijo

Reputation: 18270

Explicitly set the Content-Type of the object with image/<appropriate-subtype>.

var params = { Bucket: '', Key: '', ContentType: 'image/png',  ... }

The default Content-Type would be application/octet-stream, thus instead of rendering in the browser it is being downloaded.

Upvotes: 8

Related Questions