File Storage and Retrieval using GridFS

Geethu Suresh
Version 1
Published in
2 min readMay 27, 2022
Photo by Ilya Pavlov on Unsplash

GridFS, as discussed in my previous blog post, is an abstraction over Mongo DB that leverages the Mongo DB Driver to store files and retrieve them when needed. Let's dive into how to implement GridFS for uploading and downloading files.

Note: This post uses the C# driver and GridFS Asynchronous methods.

Initialize GridFS

The first step is to create a GridFS bucket, which will store the files and chunks collections. The collections are accessed using the bucket.

Install the NuGet package MongoDB.Driver.GridFS and create the GridFS bucket as shown below:

Creating the GridFS Bucket

Uploading the file

A file can be uploaded in the following ways:
1. Upload the file contents as a byte array.
2. Perform an upload from the source stream.
3. Write the file contents to an output stream.

The below code depicts how to write the file content to the output stream.

Uploading to an output stream

Retrieving the file

To retrieve the file, the simplest way is to acquire the file contents as a byte array.

The first method demonstrates how to obtain the file contents using the file name. This by default retrieves the newest revision of the file. Options can be used if you need to fetch a particular revision.

FindAsync method can be used to find a particular file using the required filters. The function returns all the data stored in the files collection related to the particular file. As illustrated in Method two, the id parameter from this object can be then used to download the file from GridFS.

Other options for retrieving the file include:

  1. Writing the file contents to a stream.
  2. Reading the file contents from a stream.

Conclusion

Many factors are to be taken into consideration before choosing a system, like file size, metadata storage, number of files, and so on. As stated in the previous post, GridFS is a great tool that Mongo DB provides to store large files, but it has both pros and cons. In certain scenarios, it may make sense to use GridFS, while it would not for others. Therefore, you must make an informed decision after carefully assessing the trade-offs.

About the Author:
Geethu Suresh is a Microsoft .Net Consultant here at Version 1.

--

--

Geethu Suresh
Version 1

A software engineer who enjoys meaningful conversations over a cup of coffee!