Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Serve GridFs files from mongo engine with flask
from flask import Flask, request, redirect, url_for, make_response, abort
from mongoengine.fields import get_db
from bson import ObjectId
from gridfs import GridFS
from gridfs.errors import NoFile
from <your_app> import app
def serve_gridfs_file(oid):
db = get_db()
gfs = GridFS(db)
fl = gfs.get(ObjectId(oid))
response = make_response(
response.mimetype = fl.content_type
return response
except NoFile:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment