【Ceph】基于Tornado的Ceph文件上传与下载及在线预览

来源:互联网 发布:tp路由器mac地址过滤 编辑:程序博客网 时间:2024/05/17 02:46

概述

  • 用tornado作为http服务器
  • 在处理post/get请求的方法中,调用librados(python)接口实现读写
  • 设置Content-disposition中为inline则在线浏览,attachment则下载

上传

#!/usr/bin/env python# -*- coding: UTF-8 -*-import tornado.ioloopimport tornado.webimport osimport radosclass UploadFileHandler(tornado.web.RequestHandler):    def get(self):        self.write('''        <html>          <head><title>Upload File</title></head>          <body>            <form action='file' enctype="multipart/form-data" method='post'>            <input type='file' name='file'/><br/>            <input type='submit' value='submit'/>            </form>          </body>        </html>        ''')    def post(self):        # 将上传文件写入到test池中        cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')        cluster.connect()        ioctx = cluster.open_ioctx('test')        file_metas=self.request.files['file']        for meta in file_metas:            filename=meta['filename']            ioctx.write_full(filename, meta['body'])            ioctx.close()            self.write('finished!')app=tornado.web.Application([    (r'/file',UploadFileHandler),])if __name__ == '__main__':    app.listen(3000)    tornado.ioloop.IOLoop.instance().start()

下载

#!/usr/bin/env python# -*- coding: UTF-8 -*-import tornado.ioloopimport tornado.webimport osimport radosclass DownloadFileHandler(tornado.web.RequestHandler):    def get(self, filename):        print('downloading... : ',filename)        cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')        cluster.connect()        ioctx = cluster.open_ioctx('test')        self.set_header ('Content-Type', 'application/octet-stream')        self.set_header ('Content-Disposition', 'attachment; filename='+filename)        size, timestamp = ioctx.stat(filename)        pagesize = 1024*1024        offset = 0        if pagesize > size:            pagesize = size        while offset < size:            data = ioctx.read(filename, pagesize, offset)            self.write(data)            offset += pagesize        ioctx.close()        self.finish()app=tornado.web.Application([    (r'/download/(.*)', DownloadFileHandler),])if __name__ == '__main__':    app.listen(3000)    tornado.ioloop.IOLoop.instance().start()

在线预览

#!/usr/bin/env python# -*- coding: UTF-8 -*-import tornado.ioloopimport tornado.webimport osimport radosimport mimetypesclass DownloadFileHandler(tornado.web.RequestHandler):    def get(self, filename):        print('downloading... : ',filename)        cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')        cluster.connect()        ioctx = cluster.open_ioctx('music')        ctype = mimetypes.guess_type(filename)[0]        self.set_header ('Content-Type', ctype)        self.set_header ('Content-Disposition', 'inline; filename='+filename)        size, timestamp = ioctx.stat(filename)        pagesize = 1024*1024        offset = 0        if pagesize > size:            pagesize = size        while offset < size:            data = ioctx.read(filename, pagesize, offset)            self.write(data)            offset += pagesize        ioctx.close()        self.finish()app=tornado.web.Application([    (r'/download/(.*)', DownloadFileHandler),])if __name__ == '__main__':    app.listen(3000)    tornado.ioloop.IOLoop.instance().start()

0 0
原创粉丝点击