I recently added the ability to give each one of my posts a photo. It makes the entire site a bit less boring and gives it some human feel. I found the excellent paperclip plugin from the thoughtbot guys. This allowed me to easily attach pictures, have them resized and display them along side my posts. However, I ran into some problems with managing the upload files, no worries, Amazon S3 to the rescue.
Capistrano Deployment Problems
Each time you deploy your updated rails app with capistrano, it checks out the code in a new time-stamped directory and symlinks it to a “current” directory. The problem with this setup was that each time I updated my code, the uploaded files would be wiped out. I had a few options:
- Tell paperclip to store the files above the document root
- Pretty simple, have to change the deploy recipe a bit
- Change my deploy recipe so “save” the files that have already been uploaded
- Kinda cumbersome and a little “wrong” feeling
- Tell paperclip to store the files on Amazon’s S3
- No changes to my filesystem or deploy recipe!
Paperclip Setup
In my Post model I’ve got
1 2 3 4 5 6 7 8 9 10 | has_attached_file :photo, :styles => { :tiny => "35x35", :preview => "175x175", :large => "300x300" }, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => ":attachment/:id/:style.:extension", :bucket => 'lengelzigich_blog_buckit' |
I’m telling the paperclip plugin to resize my uploaded image in a few formats and also telling it that I want to store the files using S3 rather than the file system. I’ve added my amazon S3 key/secret code to a simple yaml file and I’m all set. Almost.
Install right_aws gem
Didn’t realize this right away but needed to install the right_aws gem.
