An implementation of Mongo GridFS

10 releases

0.2.7 Oct 30, 2023
0.2.5 Mar 22, 2023
0.2.4 Feb 16, 2023
0.2.3 Jan 27, 2023
0.1.1 May 18, 2021

#152 in Database interfaces

Download history 194/week @ 2023-08-13 120/week @ 2023-08-20 90/week @ 2023-08-27 95/week @ 2023-09-03 66/week @ 2023-09-10 76/week @ 2023-09-17 50/week @ 2023-09-24 65/week @ 2023-10-01 67/week @ 2023-10-08 55/week @ 2023-10-15 148/week @ 2023-10-22 176/week @ 2023-10-29 100/week @ 2023-11-05 129/week @ 2023-11-12 125/week @ 2023-11-19 100/week @ 2023-11-26

474 downloads per month
Used in 5 crates (3 directly)

MIT license

1.5K SLoC

MongoDB GridFS Rust Driver

Crates.io docs.rs

This crate provides an implementation of Mongo GridFS on the top of mongodb's crate. This implementation only use the async/await version of mongodb.

From https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst

GridFS is a convention drivers use to store and retrieve BSON binary data (type “\x05”) that exceeds MongoDB’s BSON-document size limit of 16 MiB. When this data, called a user file, is written to the system, GridFS divides the file into chunks that are stored as distinct documents in a chunks collection. To retrieve a stored file, GridFS locates and returns all of its component chunks. Internally, GridFS creates a files collection document for each stored file. Files collection documents hold information about stored files, and they are stored in a files collection.


Uploading a document:

use mongodb_gridfs::{options::GridFSBucketOptions, GridFSBucket};
let mut bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
let id = bucket
    .upload_from_stream("test.txt", "stream your data here".as_bytes(), None)

Downloading a document:

use futures::stream::StreamExt;
use mongodb_gridfs::{options::GridFSBucketOptions, GridFSBucket, GridFSError};

let bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
let mut cursor = bucket.open_download_stream(id).await?;
let buffer = cursor.next().await.unwrap();


The following features are propagated to mongodb:

  • default
  • async-std-runtime
  • tokio-runtime

Code Status

Feature Status Notes
GridFSUploadOptions DONE contentType and aliases are not implemented
GridFSBucketOption DONE concerns not used when ensuring indexes
GridFSFindOptions DONE
GridFSDownloadByNameOptions TODO
GridFSBucket DONE
GridFSBucket . open_upload_stream DONE
GridFSBucket . open_upload_stream_with_id
GridFSBucket . upload_from_stream NO No Implementation planned
GridFSBucket . upload_from_stream_with_id NO No Implementation planned
GridFSBucket . open_download_stream DONE
GridFSBucket . download_to_stream NO No Implementation planned
GridFSBucket . delete DONE
GridFSBucket . find DONE
GridFSBucket . rename DONE
GridFSBucket . drop DONE no DropCollectionOptions used during the drop
GridFSBucket . open_download_stream_by_name
GridFSBucket . download_to_stream_by_name
indexes DONE


~782K SLoC