#mongodb #storage #file

mongo-file-center

To store perennial files and temporary files in MongoDB

20 releases

new 0.5.0 May 2, 2021
0.4.2 Jul 29, 2020
0.4.1 Jun 27, 2020
0.4.0 Mar 17, 2020
0.1.7 Nov 16, 2018

#142 in Database interfaces

Download history 22/week @ 2021-01-15 3/week @ 2021-01-22 2/week @ 2021-01-29 16/week @ 2021-02-05 134/week @ 2021-02-12 23/week @ 2021-02-19 4/week @ 2021-02-26 56/week @ 2021-03-05 21/week @ 2021-03-12 54/week @ 2021-03-19 40/week @ 2021-03-26 42/week @ 2021-04-02 6/week @ 2021-04-09 72/week @ 2021-04-16 49/week @ 2021-04-23 74/week @ 2021-04-30

173 downloads per month
Used in 2 crates

MIT license

57KB
1.5K SLoC

File Center on MongoDB

CI

This crate aims to build an easy-to-use and no-redundant file storage based on MongoDB.

For perennial files, each of them is unique in the database, and can be retrieved many times without limitation.

For temporary files, they are allowed to be duplicated, but each instance can be retrieved only one time in a minute after it is created.

The file data can be stored in a collection or GridFS. It depends on the size of data. If the size is bigger than the threshold, it stores in GridFS, or it stores in a collection. The max threshold is 16770KB. The default threshold is 255KiB.

Temporary files are suggested to store in a collection, otherwise you have to clear the garbage in GridFS.

Example

extern crate mongo_file_center;

use mongo_file_center::{FileCenter, FileData, mime};

const mongodb_uri: &str = "mongodb://localhost:27017/test_my_file_storage";

let file_center = FileCenter::new(mongodb_uri).unwrap();

let file = file_center.put_file_by_path("/path/to/file", Some("file_name"), Some(mime::IMAGE_JPEG)).unwrap();

let file_id = file.get_object_id();

let id_token = file_center.encrypt_id(&file_id); // this token is safe in public

let file_id = file_center.decrypt_id_token(&id_token).unwrap();

let r_file = file_center.get_file_item_by_id(&file_id).unwrap().unwrap();

match r_file.into_file_data() {
    FileData::GridFS(file) => {
    // do something
    }
    FileData::Collection(data) => {
    // do something
    }
}

Crates.io

https://crates.io/crates/mongo-file-center

Documentation

https://docs.rs/mongo-file-center

License

MIT

Dependencies

~17MB
~352K SLoC