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!
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.
Thanks for pointing out the typo Dan! Post has been updated.
Unfortunately it does not work for me. When I upload I get this error ‘Unable to connect to https://auth.api.rackspacecloud.com/v1.0‘. Can you help please?
@Bantab – This error is more to do with cloudfiles API access and less with paperclip.
Check if you have valid Rackspace credentials. Use curl from this link http://docs.rackspace.com/files/api/v1/cf-devguide/content/Authentication-d1e2929.html to test if your creds are good.
“Unable to connect” seems more like there was an internet connection problem but I could be wrong.