Direct image uploads to S3 with ActiveStorage
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
I've been using the edge version of Rails on a new project so we can leverage ActiveStorage and the direct image uploading functionality it provides in the browser. Unfortunately I ran into a slight CORS issue with a brand new S3 bucket where the origin host was not allowed access to upload.
The configuration for the bucket is pretty simple - just head to the CORS settings for your bucket. You'll need to enable the
PUT method, and you have the option to allow all origins or just the host of your app. You can use
* to open up for the world, or enter only the hostname you need.
The following code is a full example that shows the default code provided by S3 - a rule that allows all origins to access content with a
GET request, along with the newly added rule that will allow Rails to upload with a