Shrine::Storage::Gridfs

Provides MongoDB's GridFS storage for Shrine.

GridFS is a specification for storing and retrieving files in chunks, and is convenient when MongoDB is already used in the application.

Installation

# Gemfile
gem "shrine-gridfs", "~> 1.0"

Usage

You can initialize the GridFS storage with a Mongo connection:

require "shrine/storage/gridfs"

client = Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
Shrine::Storage::Gridfs.new(client: client)

Prefix

The default prefix (bucket name) is "fs", you can change it with the :prefix option, which is useful if you want to separate temporary and permanent storage.

Shrine.storages = {
  cache: Shrine::Storage::Gridfs.new(prefix: "temp", **options),
  store: Shrine::Storage::Gridfs.new(prefix: "fs", **options),
}

Chunk size

By default the Gridfs storage will store files in chunks of 256KB, you can change that via :chunk_size:

Shrine::Storage::Gridfs.new(chunk_size: 1*1024*1024, **options) # 1MB

Batch size

When Gridfs storage uploads or copies a file, it will ensure that no more than :batch_size of data is loaded into memory at once (defaults to 5MB).

Shrine::Storage::Gridfs.new(batch_size: 10*1024*1024, **options) # 10MB

URLs

You can generate URLs through which the GridFS files will be streamed with the download_endpoint plugin:

Shrine.plugin :download_endpoint, storages: [:store]
Rails.application.routes.draw do
  mount Shrine.download_endpoint => "/attachments"
end
user.avatar_url #=> "/attachments/store/9k30fks72j8.jpg"

Development

You can run the tests with Rake:

$ bundle exec rake test

Inspiration

This gem was inspired by refile-gridfs.

License

MIT