By Connor Shea
I’m getting a CORS error when trying to upload an image to DigitalOcean Spaces, despite it working fine in development and test.
The error is as follows:
Access to XMLHttpRequest at 'https://vglist.sfo2.digitaloceanspaces.com/KaRtoiMSiLxFpJGQHg8ZrXrC' from origin 'https://vglist.co' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
So I figured out that the reason this wasn’t working is because I needed to set up CORS, and include some headers that Rails/Spaces needed. You need to go your DigitalOcean Spaces settings, then “Add” an Advanced CORS Configuration.
Set the URL to your domain (e.g. for me it’s https://vglist.co, then allow PUT/POST (not sure if any others are necessary?). Make sure to set the allowed headers Content-Type, Content-Length, and Content-Md5.
Hopefully that helps! I got stuck on this for a while, so I wanted to create a question and answer it here :)
See also, this issue in the Rails repo: https://github.com/rails/rails/issues/32581
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.