Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing images in a Mongodb using GridFS with Meteor

Tags:

mongodb

meteor

I'm beginner in meteor and mongodb, and I'm looking for a way to store many images at once in a collection of mongodb. If there is a demo or something which can help me please give it to me. thank you !

like image 615
Abdelouhab Avatar asked Jan 05 '16 16:01

Abdelouhab


People also ask

What is the best way to store images in MongoDB?

So for storing an image in MongoDB, we need to create a schema with mongoose. For that create the file `model. js` file and define the schema. The important point here is that our data type for the image is a Buffer which allows us to store our image as data in the form of arrays.

Can you store image files in MongoDB?

No. MongoDB is NOT a good place for storing files. If you want to store files, you should use storages like Amazon S3 or Google Could Storage. The good practice is to store the files in a storage and then to just save the URI of the uploaded image in the MongoDB.

What is the use of GridFS in MongoDB?

In MongoDB, use GridFS for storing files larger than 16 MB. In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem. If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

Which of the following collection are used by MongoDB to store GridFS data?

GridFS divides a file into chunks and stores each chunk of data in a separate document, each of maximum size 255k. GridFS by default uses two collections fs. files and fs. chunks to store the file's metadata and the chunks.


1 Answers

Managing file upload is complex and hard, it depends on what kind of file and the amount of files you're dealing with

Small amount of files

If the amount and size of images or files is not very large, than you can try mongoDB GridFS, there are two famous packages that supports GridFS:

  • CollectionFS: meteor add cfs:standard-packages cfs:gridfs CollectionFS also supports server filesystem (never use this for user upload files) AWS-S3, or even dropbox. CFS is really powerful and has many useful features. However, I encountered some issues while using it, and can figure out how to get around, so I moved away... (huge number of open bug tickets doesn’t sound positive)
  • file-collection: meteor add vsivsi:file-collection lightweight compared with CFS, only support GridFS. I switched to file-collection for a while after leaving CFS, it is easier to get up and running, and IMO more predictable.

BUT! The problem with GridFS is that MongoDB is expensive to maintain, see mongolab and compose.io (previous mongohq) pricing. It's expensive because maintaining mongo is hard, even debugging is hard, and if your db crashes, your app won't work! So keeping file uploads away from mongoDB might be a better idea...

So where should I store user uploaded files?

The short answer is: S3 (or Google cloud/Azure equivalent), you can see the pricing here. S3 is stable, safe, cheap, and scales perfectly (dropbox still uses S3).

But the problem is...S3 is a lot harder to learn. I currently use slingshot to manage client side file upload (so large file uploads won't slow my web server down), and works perfectly so far (if this package doesn't work for you, you can always switch back to the official AWS SDK like this or this).

While hard to learn and setup, S3 is very flexible and powerful, so if you're allowing user to upload files to your app, storing large amount of files, want to have different user roles/permissions, or just want to prepare to scale, I think S3 is the choice.

But I want to get up and running FAST

There are some services that provides easy setup file upload and hosting, you just need to pay though, lol.

My personal favorite is Filepicker(which renamed to filestack), you can try their free plan, there's a meteor package to use too. I used it for a couple months ago when it's called filepicker.io, and works perfectly (now I switched to my own S3 though).

Conclusion

I never recommend storing any static file in mongoDB, even tiny images. For longterm and scalability, S3 + cloudfront(CDN for serving static files) is the solution. But if you're just starting to build your app, you shouldn't be wasting your time on setting up all these AWS config/policy things. I recommend starting with filepicker, just drop in the code snippet and it just works, then you can focus on building your real core features.

like image 121
DaxChen Avatar answered Sep 27 '22 23:09

DaxChen