python使用MongoDB及其GridFS进行文档管理

from pymongo import MongoClient
from gridfs import GridFS

class GFS(object):
    def __init__(self, file_db, file_table):
        self.file_db = file_db
        self.file_table = file_table

    def createDB(self):  # 连接数据库,并创建文件数据库与数据表
        client = MongoClient('localhost', 27017)
        db = client[self.file_db]
        file_table = db[self.file_table]
        return (db, file_table)

    def insertFile(self, db, filePath, query):  # 将文件存入数据表
        fs = GridFS(db, self.file_table)
        if fs.exists(query):
            print('已经存在该文件')
        else:
            with open(filePath, 'rb') as fileObj:
                data = fileObj.read()
                ObjectId = fs.put(data, filename=filePath.split('/')[-1])
                print(ObjectId)
                fileObj.close()
            return ObjectId

    def getID(self, db, query):  # 通过文件属性获取文件ID,ID为文件删除、文件读取做准备
        fs = GridFS(db, self.file_table)
        ObjectId = fs.find_one(query)._id
        return ObjectId

    def getFile(self, db, id):  # 获取文件属性,并读出二进制数据至内存
        fs = GridFS(db, self.file_table)
        gf = fs.get(id)
        bdata = gf.read()  # 二进制数据
        attri = {}  # 文件属性信息
        attri['chunk_size'] = gf.chunk_size
        attri['length'] = gf.length
        attri["upload_date"] = gf.upload_date
        attri["filename"] = gf.filename
        attri['md5'] = gf.md5
        print(attri)
        return (bdata, attri)

    # def listFile(self,db): #列出所有文件名
    #     fs = GridFS(db, self.file_table)
    #     gf = fs.list()

    # def findFile(self,db,file_table): #列出所有文件二进制数据
    #     fs = GridFS(db, table)
    #     for file in fs.find():
    #         bdata=file.read()

    def write_2_disk(self, bdata, attri):  # 将二进制数据存入磁盘
        name = "get_" + attri['filename']
        if name:
            output = open(name, 'wb')
        output.write(bdata)
        output.close()
        print("fetch image ok!")

    def remove(self, db, id):  # 文件数据库中数据的删除
        fs = GridFS(db, self.file_table)
        fs.delete(id)  # 只能是id


if __name__ == '__main__':
    gfs = GFS('fileDB', 'fileTable')
    (file_db, fileTable) = gfs.createDB()  # 创建数据库与数据表
    filePath = 'C:/Users/Administrator/Desktop/02655.jpeg'  # 插入的文件
    query = {'filename': '02655.jpeg'}
    id = gfs.insertFile(file_db, filePath, query)  # 插入文件
    id = gfs.getID(file_db, query)
    (bdata, attri) = gfs.getFile(file_db, id)  # 查询并获取文件信息至内存
    gfs.write_2_disk(bdata, attri)  # 写入磁盘
    gfs.remove(file_db,id) #删除数据库中文件
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值