Using paperclip with Amazon cloudfront

This post is in English since i guess there will be some interest in how to set up Thoughtbox's Paperclip plugin in combination with Amazon S3 / Cloudfront.

For the sake of me getting to bed a few minutes earlier, i'll assume you have set up an Amazon AWS account already and are able to alter your domain it's DNS records. (Although not necessary, it's just plain cool to have your CDN setup as cdn.yourdomain.com). Next to that, you know what Amazon S3 is all about.

So you're using Thoughtbot's paperclip gem for handling files in your Rails project? Good, now you probably have read that the location of the files stored can be specified. Not only that, but you are able to specify an Amazon S3 bucket where you can store your files too.

Next to the Paperclip gem you'll need the "aws-s3" gem that's responsible for communicating with Amazon S3.

In my case i needed a domain, cdn.eastcreative.nl that would point to Amazon cloudfront that will be used as the place to store my static files. For this to work i've created a new US based bucket also named: "cdn.eastcreative.nl". (The reason you'll need a US based bucket for this is because the aws-s3 gem doesn't support European based buckets at this time.) Next, i went to the AWS Management console where you can manage your Cloudfront distributions in a seperate tab.

Create a new distribution based on the bucket you've created. While creating this distributions you can also specify the cname's for the distribution. In this example, and in my case, i've entered 'cdn.eastcreative.nl'.

Now, when the distribution is created, select it and look for 'domain name' in the distribution details. There you'll see something like "d1ywl8dthl5rkq.cloudfront.net". (When you're not intending to use your own subdomain name for the cloudfront distribution, you can skip the DNS configuration and use the cloudfront.net address in the s3_host_alias parameter in the example below.)

In the DNS configuration tool that came with my domain, i've created a new "CNAME" record called "cdn.eastcreative.nl" that points to "d1ywl8dthl5rkq.cloudfront.net". (Keep in mind that it could take some time for the world to know your newly created subdomain.)

Now let's jump to your Rails application.

In the example below you'll find how has_attached_file is used for the model "works" i'm using. Next to the styles parameter all the other parameters are used for storing the files on S3.

has_attached_file :teaserimg,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => "s3",
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => "cdn.eastcreative.nl",
:s3_host_alias => "cdn.eastcreative.nl",
:url => ":s3_alias_url"

The s3_credentials file can be as simple as the example as shown below:

access_key_id: 1HXZXRTOMQEQ5CX8V379
secret_access_key: 9dGjZxkp1CVfVfCdpdv21+6C5Zzp14CDSrslq/Zw

Using Paperclip this way, you can see how simple it is to leverage the power of Amazon and it's Cloudfront product to create a powerfull CDN for your site.

When i deployed the change on my Ubuntu instance i got the following error while i uploaded an image;

Teaserimg /tmp/stream20091228-8464-12bnytm-0 is not recognized by the 'identify' command.

This was due to the fact that Paperclip couldn't find the appopiate command for Imagemagick. To fix this i've added the following line to the production.rb file;

Paperclip.options[:command_path] = "/usr/bin"

That was the only 'issue' i've encountered while deploying this change.

Reactie(s)

8 maanden geleden gepost door Gulf

Exactly what i needed! Thanks for the writeup.

Recent werk