Created
March 15, 2012 14:53
-
-
Save kimenye/2044606 to your computer and use it in GitHub Desktop.
Serve GridFs files from mongo engine with flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
@app.route('/files/<oid>') | |
def serve_gridfs_file(oid): | |
try: | |
db = get_db() | |
gfs = GridFS(db) | |
fl = gfs.get(ObjectId(oid)) | |
response = make_response(fl.read()) | |
response.mimetype = fl.content_type | |
return response | |
except NoFile: | |
abort(404) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment