Upload images to Amazon S3 with Paperclip on Rails
03 Dec 2016Sometimes uploading files to your server's local filesystem is not enough due to some constraints like a limited disk space and potential security issues caused by allowing users to upload files to your server. Using third party services like Amazon S3 for file storage is a good way to solve these issues. I decided to use the Paperclip gem to implement this functionality on my Rails app.
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 Amazon S3 bucket's name, access id, secret access key and region.
Demo app
I'll demonstrate how to upload files to S3 with Paperclip on Rails by building a simple demo app. You can download the source code here.
Create a new rails project by executing the command below.
Setup Figaro
Use the gem Figaro to make it easy to securely configure the rails demo application.
Add this line to your Gemfile.
Install figaro by executing the commands below.
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
Configure Paperclip to use Amazon S3
Use the official AWS and Paperclip gems.
Add these lines to the Gemfile.
Install these gems by executing the code below.
Modify and add the code below to your config/environments/development.rb and config/environments/production.rb.
Product model
Generate a Product model with an attribute "name" by executing the command below.
Add an image attachment attribute by execute the command below.
This will generate a migration like the code below.
db/migrate/20161203110020_add_attachment_image_to_products.rb
Execute the migration by running the code below.
Modify the Product model to add attachment functionality. Use the Paperclip helper method has_attached_file and a symbol with the desired name of the attachment.
app/models/product.rbProducts controller
Generate a controller for products.
Modify the ProductsController to look like the code below.
app/controllers/products_controller.rbDiplaying the image on your views
Create an index file for our homepage. Its content should look like the code below and take note of the product.image.url(:thumb). It is the reference to the resized version of the image.
app/views/products/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/products/_form.html.erbCreate our view for our show action. Notice that I use photo.image.url instead of photo.image.url(:thumb) like on our index page. This shows the original size of the uploaded.
app/views/products/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 app is finished. Execute the code below.
Go to http://localhost:3000/products and see the rails app work. Happy coding!