码迷,mamicode.com
首页 > 其他好文 > 详细

将gridFS中的图片文件写入硬盘

时间:2016-12-10 13:18:15      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:disk   png   icm   objectid   图片   sel   www   写入   print   

开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中

from pymongo import Connection
from gridfs import *
con = Connection("mongodb://admin:admin@127.0.0.1:27017")#用URI的方式建立数据库的链接,当然也有其他的方式进行授权,现在是mongodb的管理员帐号,普通帐号不知道为什么不可以,
db = con[‘repository‘]#连接到具体的数据库
fs = gridfs.GridFS(db, ‘images‘)#连接到collection,不存在的话会进行创建
fs.put(‘data.txt‘)
ObjectId(‘50b8176989ee3209bccb0b542881064151‘)#shell 返回文件在mongodb中的id,此时该数据库中会新建两个集合,images.chunk 和images.files
其中关于ObjectId的导入问题
在pymongo 2.2版本一下需要从pymongo.objectid中导入
在2.2及以上版本中从bson.objectid 中导入
Python 脚本如下
__author__ = ‘jiangyt‘
#encoding=utf-8
from pymongo import Connection
from gridfs import *
from PIL import Image
from bson.objectid import ObjectId
import StringIO
import threading, time
#文件处理系统
class GFS:
#定义connection and fs
c = None
db = None
fs = None
instance = None
locker = threading.Lock()
@staticmethod
def _connect():
if not GFS.c:
GFS.c = Connection( "mongodb://admin:admin@127.0.0.1:27017") # 建立mongodb的连接
GFS.db = GFS.c[‘maidiansha‘] #连接到指定的数据库中
GFS.fs = GridFS(GFS.db, collection=‘images‘) #连接到具体的collection中
#初始化
def __init__(self):
print "__init__"
GFS._connect()
print "server info " + " * " * 40
print GFS.c.server_info
#获得单列对象
@staticmethod
def getInstance():
GFS.locker.acquire()
try:
GFS.instance
if not GFS.instance:
GFS.instance = GFS()
return GFS.instance
finally:
GFS.locker.release()
#写入
def put(self, name, format="png",mime="image"):
gf = None
data = None
try:
data = StringIO.StringIO()
name = "%s.%s" % (name,format)
image = Image.open(name)
image.save(data,format)
#print "name is %s=======data is %s" % (name, data.getvalue())
gf = GFS.fs.put(data.getvalue(), filename=name, format=format)
except Exception as e:
print "Exception ==>> %s " % e
finally:
GFS.c = None
GFS._connect()
return gf
#获得图片
def get(self,id):
gf = None
try:
gf = GFS.fs.get(ObjectId(id))
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):
name = "./get_%s" % dic[‘name‘]
if name:
output = open(name, ‘wb‘)
output.write(data)
output.close()
print "fetch image ok!"
#获得文件列表
def list(self):
return GFS.fs.list()
#删除文件
def remove(self,name):
GFS.fs.remove(name)
if __name__== ‘__main__‘:
image_name= raw_input("input the image name>>")
if image_name: www.codesec.net
gfs = GFS.getInstance()
if gfs:
image_id = gfs.put(image_name)
print "==========Object id is %s and it‘s type is %s==========" % (image_id , type(image_id))
(data, dic) = gfs.get(ObjectId(image_id))
gfs.write_2_disk(data, dic)

将gridFS中的图片文件写入硬盘

标签:disk   png   icm   objectid   图片   sel   www   写入   print   

原文地址:http://www.cnblogs.com/cbryge/p/6155275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!