Upload and Download file from Mongo using Bottle and Flask

If you have a requirement to save and serve files, then there are at least a couple options.

  1. Save the file onto the server and serve it from there.
  2. Mongo[^n] provide GridFS[^n] store that allows you not only to store files but also metadata related to the file. For example: you can store author, tags, group etc right with the file. You can provide this functionality via option 1 too, but you would need to make your own tables and link the files to the metadata information. Besides replication of data is in built in Mongo.
Bottle

You can upload and download mongo files using Bottle[^n] like so:

import json

from bottle import run, Bottle, request, response
from gridfs import GridFS
from pymongo import MongoClient

FILE_API = Bottle()
MONGO_CLIENT = MongoClient('mongodb://localhost:27017/')
DB = MONGO_CLIENT['TestDB']
GRID_FS = GridFS(DB)

@FILE_API.put('/upload/< file_name>')
def upload(file_name):
    response.content_type = 'application/json'
    with GRID_FS.new_file(filename=file_name) as fp:
        fp.write(request.body)
        file_id = fp._id
    # If the file is found in the database then the save
    # was successful else an error occurred while saving.
    if GRID_FS.find_one(file_id) is not None: 
        return json.dumps({'status': 'File saved successfully'})
    else:
        response.status = 500
        return json.dumps({'status': 'Error occurred while saving file.'})


@FILE_API.get('/download/< file_name>')
def index(file_name):
    grid_fs_file = GRID_FS.find_one({'filename': file_name})
    response.headers['Content-Type'] = 'application/octet-stream'
    response.headers["Content-Disposition"] = "attachment; filename={}".format(file_name)
    return grid_fs_file


run(app=FILE_API, host='localhost', port=8080)

And here's the break down of the code:

Upload method:

Line 12: Sets up upload method to recieve a PUT request for /upload/<file_name> url with file_name variable holding the value that user passed in.
Line 15-17: Create a new GridFS file with name: file_name and get the content from request.body. request.body may be StringIO type or a File type because Python is smart enough to decipher the body type based on the content.

Download method:

Line 29: Find the GridFS file.
Line 30-31: Set the response Content-Type as application-octet-stream and Content-Disposition to attachment; filename=<file_name> so the client can download the file.
Line 33: Return the GridOut object. Based on Bottle documentation (below), we can return an object which has .read() method available and Bottle understands that to be a File object. Bottle handles return of File object(s) such that they can be downloaded.

File objects
Everything that has a .read() method is treated as a file or file-like object and passed to the wsgi.file_wrapper callable defined by the WSGI server framework. Some WSGI server implementations can make use of optimized system calls (sendfile) to transmit files more efficiently. In other cases this just iterates over chunks that fit into memory.

That is as simple as it gets as far as Bottle is concerned. Now on to implementing the same functionality in Flask.


Flask

You can upload/download files using Flask[^n] like so:

import json
from gridfs import GridFS
from pymongo import MongoClient
from flask import Flask, make_response
from flask import request

__author__ = 'ravihasija'

app = Flask(__name__)
mongo_client = MongoClient('mongodb://localhost:27017/')
db = mongo_client['TestDB']
grid_fs = GridFS(db)

@app.route('/upload/', methods=['PUT'])
def upload(file_name):
    with grid_fs.new_file(filename=file_name) as fp:
        fp.write(request.data)
        file_id = fp._id

    if grid_fs.find_one(file_id) is not None:
        return json.dumps({'status': 'File saved successfully'}), 200
    else:
        return json.dumps({'status': 'Error occurred while saving file.'}), 500

@app.route('/download/')
def index(file_name):
    grid_fs_file = grid_fs.find_one({'filename': file_name})
    response = make_response(grid_fs_file.read())
    response.headers['Content-Type'] = 'application/octet-stream'
    response.headers["Content-Disposition"] = "attachment; filename={}".format(file_name)
    return response

app.run(host="localhost", port=8081)

You might notice that the Flask upload and download method(s) are very similar to Bottle's. It differs only in a few places listed below:

Line 14: Routing is configured differently in Flask. You mention the URL and the HTTP methods that apply for that URL.
Line 17: Instead of request.body you use request.data to get the request content.
Line 28-31: In Flask, if you want to add additional headers, one way to do so is to "make the response" with the file content and set up the appropriate headers. Finally, return the response object.

Questions? Thoughts? Please feel free to leave me a comment below. Thank you for your time.

___ **Github repo**: https://github.com/RaviH/file-upload-download-mongo ___ #####References: [^n]: MongoDB: http://www.mongodb.org/ [^n]: GridFS: http://docs.mongodb.org/manual/core/gridfs/ [^n]: Bottle: http://bottlepy.org/docs/dev/tutorial.html [^n]: Flask: http://flask.pocoo.org/ [^n]: PyMongo GridFS doc http://api.mongodb.org/python/current/api/gridfs/index.html?highlight=gridfs#module-gridfs [^n]: Get to know GridFS: http://architects.dzone.com/articles/get-know-gridfs-mongodb ___