【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
- 【Ceph】基于Tornado的Ceph文件上传与下载及在线预览
- 20170807ceph入门(一)——ceph中rados对象创建以及文件上传与下载
- ceph的git代码下载
- 基于docker的ceph集群
- shell计算上传一个文件到CEPH的时间
- 文件的上传,下载,预览
- ceph
- ceph
- ceph与RAID的不解之缘
- ceph存储 关于OpenStack与Ceph集成的若干参考
- ceph存储 Ceph管理平台Calamari的架构与功能分析
- Tornado 与文件上传
- ceph存储 "ceph集群浅析五"Ceph的工作原理及流程
- 基于struts的文件上传与下载
- 基于springmvc的文件上传与下载
- 文件在线预览和下载的实现
- ceph存储 基于pglog的一致性协议
- 搭建基于BlueStore的Ceph环境
- 关联增加属性runtime.h
- git reset 后代码丢失 代码未commit
- IOS Dev Intro - Object Instance Type
- nyoj 324猴子吃桃
- ios蓝牙开发学习笔记(三)peripheral角色的实现
- 【Ceph】基于Tornado的Ceph文件上传与下载及在线预览
- OkHttp使用完全教程
- 关于android软键盘隐藏总结
- openscep分析
- 设置一个动态的壁纸作为你的linux桌面
- 【华为 OJ】最大差值
- FreeBuf发布2016年上半年金融行业应用安全态势报告
- Android如何在动画时将View移出父布局/如何将View显示在父布局外(android:clipChildren属性)
- hdu5532 Almost Sorted Array--最长上升子序列