How to upload images to Rails API using S3
The main reason I'm writing this blog post now is the struggle I've been through, when getting my Rails API to work with S3. The idea is to receive an image (through the API), in Base64 format, convert it to an image and store it locally (temporary) and upload it to S3. Sounds pretty easy, huh?
To start off, we'll add a few gems to our Gemfile:
Once you configure CarrierWave and CarrierWave-Base64, as well as fog, the next step is to process the received image and upload it to S3. Add the following methods in your controller:
Request example:
The image_url parameter should contain the Base64-encoded image, in the format above.
Next, in your controller's create method:
Lastly, in Fog.rb:
Make sure you set the correct AWS Keys, Region and Permissions (I got stuck on that for a long time).
If everything went perfect, you should see something similar:
This entire process could be pretty catchy, so pay attention to every little detail as it could make a significant difference. If you have any questions, please don't hesitate to contact me or leave a comment in the section below. I'll be more than happy to help you!