Using Rackspace CloudFiles with paperclip

Thoughtbot (the makers of paperclip) have not yet integrated Rackspace CloudFiles into paperclip. CloufFiles is the Rackspace CDN storage (similar to S3 but with CDN support). CDN support is very critical when we are streaming large files like videos.

Assumption: You know how paperclip works and have installed all paperclip pre-requisites like ImageMagick.

Installation

gem install cloudfiles
gem install paperclip-cloudfiles

In the config/environment.rb, its important to specify that the library we are going to us is paperclip! If you do not add the :lib, by default, Rails will search for lib/paperclip-cloudfiles and not find it and throw a dependency error.

config.gem 'cloudfiles'
config.gem "paperclip-cloudfiles", :lib => 'paperclip', :source => "http://gemcutter.org/"

Once this is setup, you need to simply configure the rackspace credentials.(Its a paid service).

Configuration

# config/rackspace.yml
  development:
    container: development-container
    username: rackspace_username
    api_key: rackspace_api_key
production:
    container: production-container
    username: rackspace_username
    api_key: rackspace_api_key</pre>

Code

Now that we have setup even the creds, its as simple as adding a couple of lines in your model:

# some model. eg. user.rb
  has_attached_file :photo
     :storage => :cloud_files,
     :cloudfiles_credentials =>  "#{RAILS_ROOT}/config/rackspace.yml",
     :path => ":attachment/:id/:timestamp_:style.:extension",
     ...

It really is this simple. The files get uploaded using paperclip standard upload and image processing if required.

Things to Note

  • S3 uses buckets and you can specify that as s3_buckets parameter. CloudFiles have a similar notion called containers. However, one containers is sufficient for all your files. We can specify the :container parameter if we want to segregate files in different containers.
  • I have not found sufficient documentation to say that providing multiple containers for different data would increase performance (worth looking into though).
  • According to the documentation, Cloud Files (strictly speaking) does not support directories, you can still use a / to  separate parts of your file name, and they will show up in the URL structure.
  • If your container does not exist, it will be automatically created.

Next post shall be for using encoding.com with CloudFiles!

5 thoughts on “Using Rackspace CloudFiles with paperclip

  1. Small correction to your guide. The :storage key needs to be :storage => :cloud_files rather than ;storage => cloud_files. Note the value of the key needs to be a symbol or it starts looking for a method in the model.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.