mongo gridfs 与 python
我想直接从 python 脚本提供来自 gridfs 的图像,但我看到的只是空白屏幕:
#!/usr/bin/env python
from pymongo import Connection
import gridfs
db = Connection().gridfs_example
fs = gridfs.GridFS(db)
f= fs.get_last_version('myimage')
print "Content-type: %s \n\n " % f.content_type
print "Content-Length: %d \n\n" % f.length
print f.read()
如果我将 f.read() 的输出写入文件,我能够看到“有效”图像 我能够从本地 FS 显示此图像:
#!/usr/bin/env python
img = open('image.jpg','rb').read()
print "Content-type: image/jpeg"
print "Content-Length: %d\n" % len(img)
print img
我做错了什么?
I want to serve image from gridfs directly from python script but only what I see is blank screen:
#!/usr/bin/env python
from pymongo import Connection
import gridfs
db = Connection().gridfs_example
fs = gridfs.GridFS(db)
f= fs.get_last_version('myimage')
print "Content-type: %s \n\n " % f.content_type
print "Content-Length: %d \n\n" % f.length
print f.read()
if I write output from f.read() to file I able to see "valid" image
I able to show this image from local FS with:
#!/usr/bin/env python
img = open('image.jpg','rb').read()
print "Content-type: image/jpeg"
print "Content-Length: %d\n" % len(img)
print img
what I did wrong?
是有问题的线。 GridFS 构造函数采用数据库实例,而不是连接对象。所以你必须做类似的事情:
希望这有帮助!
is the offending line. The GridFS constructor takes an instance of Database, not a connection object. So you would have to do something like:
Hope this helps!