I have a bucket with images and that bucket content are being distributed from a Cloudfront distribution through a OAI associated between Cloudfront distribution and S3 Bucket. My problem is when i try to display any image from that bucket, the browser try to download instead display. I read some questions here on SO about the same problem, and i saw that the metadata from the image should be image/jpeg or image/png, so i did this, but for some reason some images are try to download instead display even after that i changed the metadata content-type according the image extension. What should be doing to fix that problem?
You can try the following to solve your problem:
This is the link I created with cloud front
Now lets see how I created it: 1.Create cloudfront distribution. You can see the necessary steps here and the important note here 2.Be 100% sure that the bucket image is public and not private(u will get XML error if it private) 3. Keep everything as default values. 4.After creating cloudfront distribution wait for 15-30 min for it to be enabled. 5. Upload an image to S3 for testing. Make all the values default (recommended values). 6.Now open CloudFront domain with the image and tada! your image is now ready. Please follow me as I spent 5-6 hours solving your question!
I solved the problem that way:
def update_metadata(key, bucket, extension):
s3.copy_object(Key=key, Bucket=bucket,
CopySource={"Bucket": bucket, "Key": key},
ContentType=f"image/{extension}",
MetadataDirective="REPLACE")
parameters:
That way, the script will overwrite the current Content Type and image will be displayed when request through browser. The library used was boto3.
Important point : After run the changes, it will be necessary create an invalidation on Cloudfront distribution for everything works well.
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.