Month: September 2009

Quick fix for moving to Vlad the Deployer 2.0.0 with git

Posted by – September 9, 2009

I just upgraded my dev machine to version 2.0.0 of Vlad the Deployer. I got one unexpected error -- "Please specify the deploy path via the :deploy_to variable" -- but here is how I fixed it:

git support is now a separate gem, so remember to run

sudo gem install vlad-git

I also had some problems when version 1.4.0 was also on my machine, so I uninstalled that one with:

sudo gem uninstall vlad -v "1.4.0"

Using Amazon’s CloudFront with Rails & Paperclip

Posted by – September 8, 2009

It took me a bit of experimentation, and I never found an example in a single place that showed how to set it up exactly how I wanted, so here is my code in my model for storing images used by the ArtCat calendar on Amazon S3. I am using Paperclip version 2.3.1.

First you will need to set up the distribution in Amazon for your given bucket, so that you have a URL to use for the :s3_host_alist value. I also set up a CNAME so that I can use a nice url like calcdn.artcat.com.

Note that I don't want to store any images other than my resized ones, so my :default_style is set to :original. Some of these values are actually constants in my config files, but I've replaced those here to make it more clear.

    has_attached_file :image,
      :storage => 's3',
      :s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml",
      :bucket => 'artcal-production',
      :s3_host_alias => 'calcdn.artcat.com',
      :url => ':s3_alias_url',
      :path => "images/:class/:id_:timestamp.:style.:extension",
      :styles => { :thumb  => '60x60#', :medium => '270x200#', :original  => '600x600>' },
      :default_style => :original,
      :default_url => 'http://cdn1.artcat.com/pixel.gif',
      :s3_headers => { 'Expires' => 1.year.from_now.httpdate },
      :convert_options => { :all => '-strip -trim' }

Note that you do NOT have to set the ActionController::Base.asset_host to your CNAME for images. Paperclip just handles it as expected for these images.

You'll notice an interpolation in the :path that is not standard. Thanks to this Intridea Company Blog post I learned that I will need to change my image names when they are updated. CloudFront will not update my image due to that Expires header I set above for a whole year, which is not what we want to happen. I solved this by including the timestamp based on the updated_at value for the image. Based on that Intridea post, this is the code I added to config/initalizers/paperclib.rb.

Paperclip.interpolates(:timestamp) do |attachment, style|
  attachment.instance_read(:updated_at).to_i  
end

At first I was storing the images on the file system and serving them via Apache. Moving them to CloudFront improved my page load times by at least 50%, and means that I don't have to run as powerful as server to handle a lot of traffic on this image-heavy site as I might otherwise need.