Upload images to Amazon S3 with Carrierwave on Rails
09 Jan 2017While searching for ways to upload images to Amazon S3 on Rails, I stumbled upon two gems that simplifies this task, Paperclip and Carrierwave. I tried Paperclip first and made an article about it here. A senior colleague of mine recommended Carrierwave, so I tried to give it a shot and it was worth it. It is much cleaner to implement because it requires the user to create a helper class for uploading. The model just needs to mount the helper class. This separates the code for uploading unlike on Paperclip where the code for uploading is included in the model and requires the user to create a migration.
Setup Amazon S3
This article does not cover setting up an Amazon S3 bucket. This article assumes that you have already setup your Amazon S3 bucket and familiar with your AWS credentials. You should know your bucket's name, host name, AWS access id, AWS secret access key and AWS region.
Photos app
I'll demonstrate how to upload files to S3 with Carrierwave on Rails by building a simple photos app. You can download the source code here.
Create a new rails project by executing the command below.
Generate and migrate a Photo model with attributes "name" and "image" by executing the command below.
Setup Figaro
Use the gem Figaro to make it easy to securely configure Rails applications.
Add figaro gem to your Gemfile.
Install figaro.
This will generate an application.yml file at the config directory. The file's content should look like the code below. Replace the value with your Amazon S3 bucket's credentials.
config/application.yml
This file contains the credentials for your S3 bucket and shouldn't be added to your git repository, fortunately figaro adds this file to your .gitignore file upon installation. A good practice for using figaro is adding an application.yml.template file to your repository's config directory so that other users can just copy its contents when setting up their application.yml on their new development environment.
config/application.yml.template
Setup Carrierwave
Add these lines to the Gemfile.
Carrierwave is a gem for uploading files. MiniMagick is a gem for resizing images and Fog::AWS is a gem that adds support for Amazon Web Services. Install these gems by executing the command below.
Generate an image uploader by executing the command below.
Executing the command above generates an ImageUploader class at app/uploaders/image_uploader.rb
Carrierwave supports the gems MiniMagick and RMagick for photo resizing. In this case I chose to use MiniMagick because it is more lightweight compared to RMagick. Set fog as the storage option instead of file, because we are gonna use Amazon S3 for storage. The method stor_dir sets where the file should be uploaded.
Mount the image uploader on the Photo model.
app/models/photo.rb
Configure Carrierwave to use S3 credentials and stop it from using SSL when accessing the bucket.
config/initializers/carrierwave.rb
Controller and views
Create a Photo controller by executing the command below.
Modify the PhotosController to look like the code below.
app/controllers/photos_controller.rbCreate an index file for our homepage. Its content should look like the code below and take note of the photo.image.thumb. It is the reference to the resized version of the image.
app/views/photos/index.html.erbCreate a partial form for our new and edit actions. The file_field will be used for selecting a file to upload. Take note of :html => {:multipart => true}, this is necessary to upload the image in multiple chunks.
app/views/photos/_form.html.erbCreate our view for our show action. Notice that I use photo.image instead of photo.image.thumb like on our index page. This shows the original size of the uploaded.
app/views/photos/show.html.erb app/views/photos/new.html.erb app/views/photos/edit.html.erbMake sure we have configured our routes properly.
config/routes.rbThe photos app is finished. Execute the code below.
Go to http://localhost:3000/photos and see the rails photo app work. Happy coding!