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