• mongodb操作文件


    mongodb操作文件,主要是通过GridFS类。存储文件主要存放在fs中,其中的fs是数据库默认的。并且GridFS是直接与数据库打交道,与collection集合无关。

     ===============

    https://docs.mongodb.com/manual/core/gridfs/

    When to Use GridFS

    In MongoDB, use GridFS for storing files larger than 16 MB.

    =======================

    '''
    Created on 2013-8-6
    class mongoInsert
    @author: tree
    '''
    __metaclass__ = type
    
    import os
    from pymongo.database import Database
    import time
    import gridfs
    
    class mongoImg(object):
        """mongoInsert is a class for inserting document
        
        
        """
        def __init__(self, database, dir):
            """Create a new instance of :class:mongoInsert
            :Parameters:
              - `database`: database to use
              - `dir` : directory of document 
              """
            if not isinstance(database, Database):
                raise TypeError("database must be an instance of Database")
            if len(dir) < 1:
                raise TypeError("dir must be an string of directory")
            
    #         self.__con = Connection()
            self.__imgdb = database
            self.__imgfs = gridfs.GridFS (self.__imgdb)
            self.__dir = dir
            self.__filelist=[]
    
        #save filepath in list.txt
        def __dirwalk(self,topdown=True):
            """traverse the documents of self.__dir and save in self.__filelist
            """
            sum=0
            self.__filelist.clear()
            
            for root,dirs,files in os.walk(self.__dir,topdown):
                for name in files:
                    sum+=1
                    temp=os.path.join(root,name)
                    self.__filelist.append(temp)
            print(sum)
    
        #insert image 
        def insert(self):
            """insert images in mongodb
            """
            self.__dirwalk()
    
            tStart = time.time()        
            for fi in self.__filelist:       
                with open (fi,'rb') as myimage:
                    data=myimage.read()                  
                    self.__imgfs.put(data, content_type = "jpg", filename =fi)
        
            tEnd =time.time ()
            print ("It cost %f sec" % (tEnd - tStart))
            
        #get image by filename
        def getbyname(self,filename,savepath):
            """get img from mongdb by filename
            """
            if len(savepath) < 1:
                raise TypeError("dir must be an string of directory")
            dataout=self.__imgfs.get_version(filename)
            try:
                imgout=open(savepath,'wb')
                data=dataout.read()
                imgout.write(data)
            finally:
                imgout.close()
    

      

  • 相关阅读:
    将其他js类库制作成seajs模块
    Jquery 数组操作
    jquery 数组 添加元素
    Asp.net core MVC paypal支付、回调——app支付
    最近接触的几种APP支付方式——支付宝支付
    最近接触的几种APP支付方式——信用卡支付AuthorizeNet
    最近接触的几种APP支付方式——微信app支付
    微信选择图片、上传图片、下载图片、扫一扫接口调用源码
    Oracle连接查询语句
    在ps中,怎么把图片的一部分剪切出来,创建一个新的图层?
  • 原文地址:https://www.cnblogs.com/testzcy/p/9802152.html
Copyright © 2020-2023  润新知