Paperclip and Amazon S3

Thursday June 19th, 2008 (permalink)
Paperclip and Amazon S3

Photo Credit

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:

  1. Tell paperclip to store the files above the document root
    • Pretty simple, have to change the deploy recipe a bit
  2. Change my deploy recipe so “save” the files that have already been uploaded
    • Kinda cumbersome and a little “wrong” feeling
  3. 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

  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

Photo from flickr

Posted In: Ruby on Rails

Comments

Jon Yurek said:

For what it’s worth, we typically keep our files above the document root and use symlinks (which svn handles just fine) to make sure everything lines up. We haven’t had to change our capfiles to accommodate this, but I admit it wouldn’t work if you were on Windows.

Thanks for the kind words! I’m glad you like paperclip.

2008-06-19 13:20:36

Clayton said:

In terms of paperclip saving things above the document root I’m imagining having paperclip save them in a directory that’s not already symlinks as part of my deployment process, hence the need for modifying the recipe. However, if I stored them on the file system in for example "images" which I’m already symlinking, it would be fine.

Thanks for the comment.

2008-06-19 16:15:03

Dave said:

I ran into a similar problem with page_attachments under Radiant CMS. For what it’s worth, I just added the following to my cap deploy.rb:

  1. post deployment hook
    task :after_symlink do link_page_attachments
    end

desc "Link current release public/page_attachments directory to page_attachments in shared directory"
task :link_page_attachments, :roles => :app do run "ln -s #{shared_path}/page_attachments #{current_path}/public/page_attachments"
end

Basically, I moved all my files into the "shared" folder, then wrote a small task to link the files. This gets called by the after_symlink hook, which is called every time you do an update with cap.

But S3 is also a good solution, and we’re moving that way on another large project we have under development right now.

Hope this helps!

2008-07-23 12:03:43

Leave a Response

 (required)
 (required, never shared)

 (required)

(required)

Let's Chat

E-Mail Me

Categories

Recent Posts

Subscribe

RSS Icon