Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3: No Access-Control-Allow-Origin header is present on the requested resource

I've got a S3 bucket in the eu-west-1 region, let's call it my-bucket.

In that bucket there's a picture, let's call it some-file.jpg.

If I access both these urls through the browser, I can retrieve that picture (all objects in the bucket are public) (remember that these are examples, not real-life urls):

https://my-bucket.s3.amazonaws.com/some-file.jpg

https://s3-eu-west-1.amazonaws.com/my-bucket/some-file.jpg

However, I'm trying to get some info on that picture, and for that I'm using vibrant.js, which attempts to retrieve the file.

However, it fails when the url is: https://my-bucket.s3.amazonaws.com/some-file.jpg, with the following CORS error:

Access to Image at 'https://my-bucket.s3.amazonaws.com/some-file.jpg' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' is therefore not allowed access

I've made sure that the bucket's CORS policy accepts all origins:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

My guess is that https://s3-eu-west-1.amazonaws.com/my-bucket/some-file.jpg sends the Access-Control-Allow-Origin and the other one doesn't. How can I solve this?

like image 959
sauronnikko Avatar asked Jan 10 '18 20:01

sauronnikko


People also ask

How do I fix CORS policy no Access-Control allow origin?

To allow any site to make CORS requests without using the * wildcard (for example, to enable credentials), your server must read the value of the request's Origin header and use that value to set Access-Control-Allow-Origin , and must also set a Vary: Origin header to indicate that some headers are being set ...

How do you solve S3 CORS file error with Access-Control allow Origin header block?

In the new (2020) S3 interface/dashboard, you need to write the header as a JSON. This code will fix the S3 Access-Control-Allow-Origin Header, allowing for GET requests from any domain. This code is placed in the Cross-origin resource sharing (CORS) section of the permissions tab for your specific bucket.


1 Answers

Did you clear your browser cache after updating the CORS headers

The problem I had was that Chrome was caching the image with the headers (not containing the CORS data), so no matter what I tried to change in AWS, I would not see my CORS headers.

After clearing Chrome cache and reloading the page, the image had the expected CORS Headers

like image 83
Tonio Avatar answered Sep 21 '22 11:09

Tonio