Streaming Images from MongoDB using Golang
Recently I had a client that has a fairly large image library stored in MongoDB using GridFS. I was building them an application which provided an interface between their front-end website and their new back-office system, which provides accounting services among other things. Part of this application needed to retrieve images from this MongoDB system and stream them to the browser so their front-end website could use them.
Now, if you aren’t familiar with MongoDB, it is a document-based database with powerful querying capabilities. GridFS is a system designed for storing files larger than the 16MB limit imposed on documents in MongoDB, and it is a part of MongoDB. So, for example, if I need to define an image, it may look something like this in JSON format, which just happens to be how one works with MongoDB data (well, BSON, but let’s not get technical here).
We’ll use this sample moving forward to represent an image record for demonstration purposes. This sample should be pretty familiar looking for anyone familiar with the JSON format. Most of this would be expected information…