Class: CarrierWave::Storage::GridFS

Inherits:
Abstract
  • Object
show all
Defined in:
lib/carrierwave/storage/grid_fs.rb

Overview

The GridFS store uses MongoDB’s GridStore file storage system to store files

There are two ways of configuring the GridFS connection. Either you create a connection or you reuse an existing connection.

Creating a connection looks something like this:

  CarrierWave.configure do |config|
    config.storage = :grid_fs
    config.grid_fs_host = "your-host.com"
    config.grid_fs_port = "27017"
    config.grid_fs_database = "your_dbs_app_name"
    config.grid_fs_username = "user"
    config.grid_fs_password = "verysecret"
    config.grid_fs_access_url = "/images"
  end

In the above example your documents url will look like:

   http://your-app.com/images/:document-identifier-here

When you already have a Mongo connection object (for example through Mongoid) you can also reuse this connection:

CarrierWave.configure do |config|
  config.storage = :grid_fs
  config.grid_fs_connection = Mongoid.database
  config.grid_fs_access_url = "/images"
end

Defined Under Namespace

Classes: File

Instance Attribute Summary

Attributes inherited from Abstract

#uploader

Instance Method Summary collapse

Methods inherited from Abstract

#identifier, #initialize, #rename!

Constructor Details

This class inherits a constructor from CarrierWave::Storage::Abstract

Instance Method Details

#retrieve!(identifier) ⇒ Object

Retrieve the file from MongoDB’s GridFS GridStore

Parameters

identifier (String)

the filename of the file

Returns

CarrierWave::Storage::GridFS::File

a sanitized file



130
131
132
# File 'lib/carrierwave/storage/grid_fs.rb', line 130

def retrieve!(identifier)
  CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
end

#store!(file) ⇒ Object

Store the file in MongoDB’s GridFS GridStore

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::SanitizedFile

a sanitized file



113
114
115
116
117
# File 'lib/carrierwave/storage/grid_fs.rb', line 113

def store!(file)
  stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
  stored.write(file)
  stored
end