Getting CORS error while uploading files to Amazon S3 using presigned url

Viewed 6834

I am trying to upload a file to Amazon S3 using the angular client. I have generated Presigned URL using NodeJs application server. While uploading a file to presigned URL, I am getting an Error as:

"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access."

I have added following CORS configuration to my bucket permissions.

<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
4 Answers

My issue was that even after doing the following I got the same CORS error - for some reason - getSignedUrl returned a url like so:

https://my-bucket.s3.us-west-2.amazonaws.com/bucket-folder/file.jpg

I've removed the region part - us-west-2 part - and that fixed it ‍♂️

So instead it is now

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

Some browsers do not support CORS requests from Localhost, such as Chrome. Try using lvh.me:4200 for testing.

While it's true that localhost may not be supported by your browser as a valid origin to match against AllowedOrigin, you might also need to check that the content being fetched from S3 actually correctly has the correct headers that you expect and that your bucket is setup correctly. You can do so via chrome developer tools: https://developers.google.com/web/tools/chrome-devtools/network-performance/reference#headers

http://127.0.0.1 is also a valid domain for localhost (on most OSes) that should be compatible with most browsers for matching against AllowedOrigin.

Related