- Tutorials >
- GridFS
GridFS¶
On this page
The driver provides a clean and simple interface to work with storage of chunked files in the database, also known as the pattern “GridFS”. The API allows you to either work with Grid::File objects or with read and write streams.
Creating a GridFS object (“Grid::FSBucket”)¶
You can create a GridFS object by calling fs
on a database, with optional
arguments. fs
returns a Grid::FSBucket
object.
The options that GridFS::Bucket
supports are:
Option | Description |
---|---|
:bucket_name |
The name of the GridFS Bucket. Default is fs . |
:fs_name |
The name of the GridFS Bucket. Takes precedence over bucket_name . Default is fs . |
:chunk_size |
Specifies the size of each file chunk in the database. |
:write |
The write concern used when uploading files. |
:read |
The read preference used when downloading files. |
For example, you can create a GridFS bucket object with a particular read preference:
Working with write streams¶
To upload a file to GridFS using a write stream, you can either open a stream and write to it directly or write the entire contents of an IO object to GridFS all at once.
To open an upload stream and write to it:
To upload the entire contents of an IO object in one call:
Working with read streams¶
To download a file from GridFS using a read stream, you can either open a read stream and read from it directly or download the entire file all at once.
To open a download stream and read from it:
To download the file all at once and write it to an IO object:
You can also download a file specified by a name and (optionally)
revision number. Revision numbers are used to distinguish
between files sharing the same name, ordered by date of upload. The revision number passed to
open_download_stream_by_name
can be positive or negative.
To download the entire contents of the file specified by name and (optionally) revision number:
Finding file metadata¶
You can retrieve documents containing metadata about files in the GridFS files collection.
Deleting files¶
You can delete a file by id.
Working with Grid::File objects¶
This object can be used to wrap a file to be inserted into the database using GridFS and the object that is retrieved.
To create a file with raw data:
To create a file from a Ruby File
object:
To change file options such as chunk size, pass options to the constructor:
The following is a full list of the available options that files support.
Option | Description |
---|---|
:chunk_size |
Sets the size of each file chunk in the database. |
:content_type |
Set a content type for the file. |
:filename (Required) |
The file name. |
:upload_date |
The date the file was uploaded (stored). |
Inserting Files¶
Files can be inserted into the database one at a time. File chunks are inserted
by default into the fs.chunks
collection and file metadata is inserted into the
fs.files
collection.
To insert into collections with a name prefix other than fs, access the
filesystem with a :fs_name
option.
Note that the first time a file is inserted, it will create the required index
for you on the chunks
collection. This index is a compound index:
Files can also be streamed as an alternative to a direct insert.
Finding Files¶
To retrieve a file from the database, call find_one
with the appropriate filter.
Files can also be streamed as an alternative to a direct find.
Deleting Files¶
To delete a file, pass the file object to delete_one
.