CarrierWave gives you a store for permanent storage, and a
cache for temporary storage. You can use different stores, at the
moment a filesystem store, an Amazon S3 store, a Rackspace Cloud Files
store, and a store for MongoDB's GridFS are bundled.

Most of the time you are going to want to use CarrierWave together with an
ORM. It is quite simple to mount uploaders on columns in your model, so you
can simply assign files and get going:

ActiveRecord, DataMapper, Sequel, Mongoid

Make sure you are loading CarrierWave after loading your ORM, otherwise
you'll need to require the relevant extension manually, e.g.:

require 'carrierwave/orm/activerecord'

Add a string column to the model you want to mount the uploader on:

add_column :user, :avatar, :string

Open your model file and mount the uploader:

class User
mount_uploader :avatar, AvatarUploader
end

This works the same with all supported ORMs.

Now you can cache files by assigning them to the attribute, they will
automatically be stored when the record is saved.

When this uploader is used, an uploaded image would be scaled to be no
larger than 800 by 800 pixels. A version called thumb is then created,
which is scaled and cropped to exactly 200 by 200 pixels. The uploader
could be used like this:

One important thing to remember is that process is called before
versions are created. This can cut down on processing cost.

It is possible to nest versions within versions:

class MyUploader < CarrierWave::Uploader::Base
version :animal do
version :human
version :monkey
version :llama
end
end

Making uploads work across form redisplays

Often you'll notice that uploaded files disappear when a validation
fails. CarrierWave has a feature that makes it easy to remember the
uploaded file even in that case. Suppose your user model has an
uploader mounted on avatar file, just add a hidden field called
avatar_cache. In Rails, this would look like this:

Providing a default URL

In many cases, especially when working with images, it might be a good idea
to provide a default url, a fallback in case no file has been uploaded. You
can do this easily by overriding the default_url method in your
uploader:

Recreating versions

You might come to a situation where you want to retroactively change a
version or add a new one. You can use the recreate_versions! method to
recreate the versions from the base file. This uses a naive approach which
will reupload and process all versions.

instance = MyUploader.new
instance.recreate_versions!

Or on a mounted uploader:

User.all.each do |user|
user.avatar.recreate_versions!
end

Configuring CarrierWave

CarrierWave has a broad range of configuration options, which you can
configure, both globally and on a per-uploader basis:

Testing CarrierWave

It's a good idea to test you uploaders in isolation. In order to speed
up your tests, it's recommended to switch off processing in your tests,
and to use the file storage. In Rails you could do that by adding an
initializer with:

if Rails.env.test?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end
end

If you need to test your processing, you should test it in isolation, and
enable processing only for those tests that need it.

CarrierWave comes with some RSpec matchers which you may find useful:

require 'carrierwave/test/matchers'
describe MyUploader do
before do
MyUploader.enable_processing = true
@uploader = MyUploader.new(@user, :avatar)
@uploader.store!(File.open(path_to_file))
end
after do
MyUploader.enable_processing = false
end
context 'the thumb version' do
it "should scale down a landscape image to be exactly 64 by 64 pixels" do
@uploader.thumb.should have_dimensions(200, 200)
end
end
context 'the small version' do
it "should scale down a landscape image to fit within 200 by 200 pixels" do
@uploader.small.should be_no_larger_than(200, 200)
end
end
it "should make the image readable only to the owner and not executable" do
@uploader.should have_premissions(0600)
end
end

Using Amazon S3

Older versions of CarrierWave used the aws-s3 and
right_aws libraries. The Aws is now used meaning european
buckets are supported out the box. Ensure you have it installed:

gem install aws

You'll need to configure a bucket, access id and secret key like this:

Since GridFS doesn't make the files available via HTTP, you'll need
to stream them yourself. In Rails for example, you could use the
send_data method. You can tell CarrierWave the URL you will serve
your images from, allowing it to generate the correct URL, by setting eg:

Using RMagick

If you're uploading images, you'll probably want to manipulate them
in some way, you might want to create thumbnail images for example.
CarrierWave comes with a small library to make manipulating images with
RMagick easier, you'll need to include it in your Uploader:

The RMagick module gives you a few methods, like
CarrierWave::RMagick#resize_to_fill which manipulate the image
file in some way. You can set a process callback, which will call
that method any time a file is uploaded.

Using ImageScience

Using MiniMagick

MiniMagick is similar to RMagick but performs all the operations using the
'mogrify' command which is part of the standard ImageMagick kit.
This allows you to have the power of ImageMagick without having to worry
about installing all the RMagick libraries.

License

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Development

If you want to run the tests (and you should) it might be convenient to
install the development dependencies, you can do that with:

sudo gem install carrierwave --development

CarrierWave is still young, but most of it is pretty well documented. It is
also extensively specced, and there are cucumber features for some common
use cases. Just dig in and look at the source for more in-depth explanation
of what things are doing.