A handler that can serve content from GridFS, very similar to tornado.web.StaticFileHandler.
db = motor.MotorClient().open_sync().my_database
application = web.Application([
(r"/static/(.*)", web.GridFSHandler, {"database": db}),
])
By default, requests’ If-Modified-Since headers are honored, but no specific cache-control timeout is sent to clients. Thus each request for a GridFS file requires a quick check of the file’s uploadDate in MongoDB. Override get_cache_time() in a subclass to customize this.
See also
MongoDB and GridFS
See also
Override to customize cache control behavior.
Return a positive number of seconds to trigger aggressive caching or 0 to mark resource as cacheable, only. 0 is the default.
Overridable method to choose a GridFS file to serve at a URL.
By default, if a URL pattern like "/static/(.*)" is mapped to this GridFSHandler, then the trailing portion of the URL is used as the filename, so a request for “/static/image.png” results in a call to get_gridfs_file with “image.png” as the path argument. To customize the mapping of path to GridFS file, override get_gridfs_file and return a Future MotorGridOut from it.
For example, to retrieve the file by _id instead of filename:
class CustomGridFSHandler(motor.web.GridFSHandler):
def get_gridfs_file(self, fs, path):
# Path is interpreted as _id instead of name.
# Return a Future MotorGridOut.
return fs.get(file_id=ObjectId(path))
Parameters : |
|
---|
Changed in version 0.2: get_gridfs_file no longer accepts a callback, instead returns a Future.
For subclass to add extra headers to the response