Note: This guest post hails from community contributor Trevor Turk. Trevor is a chess-playing machine of the late 18th century, promoted as an automaton but later proved a hoax. Trevor tweets as @trevorturk and blogs too.
CarrierWave is self-described as a "classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks." Although I've head it referred to as "a new kid on the block" it's actually quite an old gem. The initial checkin is from August 2008 and the first release was in March 2009. The original name was Merb::Upload and it started without support for Rails.
The fact that CarrierWave began its life as a Merb plugin may help explain its modularity, flexibility, and extensibility.
Thanks to fog, it has support for Amazon S3, Rackspace Cloud Files, and Google Storage for Developers. It also supports plain old file storage and MongoDB's GridFS store. There's ORM support available for ActiveRecord, Mongoid, DataMapper, Sequel, Mongo Mapper, CouchDB, and more. Image processors are available for RMagick, ImageScience, MiniMagick.
Let's see this modularity first hand by building up a CarrierWave uploader from scratch.
To begin with, we'll install CarrierWave:
Then, we can make the world's shortest uploader:
Even at this point, we can start saving files:
The uploader houses the logic for uploading files in self-contained classes, so this is all we need to get started.
It's dead easy to store files on Amazon S3 with fog. First, install the fog gem:
Then configure the fog_credentials and set the uploader's storage to fog:
Configuring fog to use Rackspace Cloud Files or Google Storage for Developers is so easy I hesitate to even mention it. This is all we'd need to change to use Rackspace:
Suppose we want to have image thumbnails. First, we need to install RMagick:
Then, we just add a version block to our uploader:
To begin, we'll need to install these new gems:
Then, we'll create the app. It's so small (55 lines) that I'll just include the whole thing here:
The app can be run like so:
There's a lot more I could have covered in this post, but my hope is that this brief illustration of the modularity, flexibility, and extensibility of CarrierWave will appeal to you as it did to me.
If you're ready for more, check out:
- the readme
- the wiki
- the Mocking fog when using it with CarrierWave post from Mike Gehard
- and the How to set up CarrierWave for local storage with AppCloud documentation for Engine Yard AppCloud users.
I'm sure you'll enjoy using CarrierWave as much as I have!