-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbDemo.py
118 lines (99 loc) · 3.21 KB
/
MongoDbDemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#encoding=utf-8
import StringIO
import threading
from pymongo import Connection
from gridfs import *
from PIL import Image
from bson.objectid import ObjectId
# 文件处理系统
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://127.0.0.1:27017") # 建立mongodb的连接
GFS.db = GFS.c['hw-sq'] # 连接到指定的数据库中
GFS.fs = GridFS(GFS.db, collection='fs') # 连接到具体的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 = u'F:\gongxiang\lptp\济南楼盘\houses_type_pic\\2-221-90-A.jpg'
if image_name:
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)