Serving Images with MongoDB, GridFS and Flask

Recently, I’ve had the pleasure of accumulating over 1 million thumbnail and preview images on my drive. I like having as many files on my disk as the next guy, but I thought since I’m using MongoDB anyway, why not use GridFS to store the images!

The trick was to fetch an image at a URL, save it into GridFS, then be able to serve it back out – all without temp files or creating individual files for every image. Here’s a recipe that does it all!

I didn’t have much luck using the plain StringIO class and it’s tricky to get the right interfaces working together – some stream readers are more demanding than others…

One comment on “Serving Images with MongoDB, GridFS and Flask

  1. Thanks for the direction and I like this idea a lot. Can you please show how it can be used in a web browser to render the thumbnails?

Leave a Reply