使用GridFS上传下载图片以及其他文件

  MongoDB所带的GridFS是极为方便的文件管理系统,MongoDB的Shell语言与Python的语言风格非常像,写起来非常方便。重点是需要用StringIO将文件装换为二进制保存。主程序是一个同步图片的小范例。

 

#encoding=utf-8
import sys

if sys.getdefaultencoding() != 'utf-8':
    reload(sys)
    sys.setdefaultencoding('utf-8')
__author__ = 'lxc'

import pymongo
from gridfs import *
from PIL import Image
import StringIO
import os

#文件处理系统
class GFS:
    def __init__(self, db):
        self.myFs = GridFS(db, 'images')


    #写入
    def put(self, file_path):
        gf = None
        try:
            data = StringIO.StringIO()
            image = Image.open(file_path)
            form = image.format
            image.save(data, form)
            gf = self.myFs.put(data.getvalue(), filename=file_path.split('\\')[-1], format=form)
        except Exception as e:
            print  e
        finally:
            return gf


    #获得
    def get(self, name):
        gf = None
        try:
            gf=self.myFs.get_version(name)
            im = gf.read()                  #read the data in the GridFS
            dic = {}
            dic["chunk_size"] = gf.chunk_size
            dic["metadata"] = gf.metadata
            dic["length"] = gf.length
            dic["upload_date"] = gf.upload_date
            dic["name"] = gf.name
            dic["content_type"] = gf.content_type
            dic["format"] = gf.format
            return (im, dic)
        except Exception, e:
            print e
            return (None, None)
        finally:
            if gf:
                gf.close()


    #将gridFS中的图片文件写入硬盘
    def write_2_disk(self, data, dic,path=None):
        path=path+dic['name']
        if path:
            output = open(path, 'wb')
            output.write(data)
            output.close()

    #获得文件列表
    def list(self):
        return self.myFs.list()

    #删除文件
    def remove(self, name):
        self.myFs.remove(name)


if __name__ == '__main__':
        #DEMO:上传下载源文件目录下的image文件夹
    client = pymongo.MongoClient()
    db = client.test
    gfs = GFS(db)
    local_image_path = os.getcwd() + "\\image\\"
    if not os.path.exists(local_image_path):
        os.makedirs(local_image_path)
    local_pic_lists = os.listdir(local_image_path)
    remote_pic_lists = gfs.list()
    up = 0
    for local_pic in local_pic_lists:
        if local_pic not in remote_pic_lists:
            gfs.put(local_image_path + local_pic)
            up += 1
    down = 0
    for remote_pic in remote_pic_lists:
        if remote_pic not in local_pic_lists:
            (data, dic) = gfs.get(remote_pic)
            gfs.write_2_disk(data, dic, local_image_path)
            down += 1
    print up, down

 

转载于:https://www.cnblogs.com/leisurely/p/3390929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值