微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asyncgridfs MongoDB GridFS 开发库

程序名称:asyncgridfs

授权协议: MIT

操作系统: 跨平台

开发语言: Python

asyncgridfs 介绍

Tornado 非阻塞的 MongoDB GridFS 开发库

示例代码

import asyncmongo
import tornado.web
from asyncgridfs import GridFS

class Handler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(self, '_db'):
            self._db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='test')
        return self._db

    @tornado.web.asynchronous
    def get(self):
        fid = self.get_argument('fid')
        fs = GridFS(self.db)
        fs.get(ObjectId(fid),callback=self._on_get)


    @tornado.web.asynchronous
    def post(self):
        f = self.request.files['imgFile'][0]
        content = f.pop('body')
        content_type = f.pop('content_type')
        filename = f.pop('filename')

        fs = GridFS(self.db)
        fs.put(content, contentType=content_type, filename=filename, callback=self._on_post)


    def _on_get(self, fileobj):
        self.set_header('Content-Type', fileobj['contentType'])
        self.write(fileobj['data'])
        self.finish()

    def _on_put(self, _id):
        self.write(str(_id))
        self.finish()

asyncgridfs 官网

https://github.com/comger/asyncgridfs

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐