Skip to content

Instantly share code, notes, and snippets.

@jcarlosgarcia
Created September 14, 2015 20:53
Show Gist options
  • Save jcarlosgarcia/7c06989ab3dd4d931a76 to your computer and use it in GitHub Desktop.
Save jcarlosgarcia/7c06989ab3dd4d931a76 to your computer and use it in GitHub Desktop.
GridFS and Bottle example
import bottle
import pymongo
import gridfs
from bottle import response
# this is the handler for the default path of the web server
@bottle.route('/img/<filename>')
def show_img(filename):
# connect to mongoDB
connection = pymongo.MongoClient('localhost', 27017)
# attach to test database
db = connection.test
fs = gridfs.GridFS(db, "images")
fsout = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return fsout
bottle.run(host='localhost', port=8082, reloader=True)
@jcarlosgarcia
Copy link
Author

Please note that you have to insert your files like this:

grid = gridfs.GridFS(db, "images")
fin = open(file_used, "r")

_id = grid.put(fin, filename=file_used)
fin.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment