mongodb存储二进制数据的二种方式——binary bson或gridfs
2017-03-07 11:03
417 查看
python 版本为2.7
mongodb版本2.6.5
使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们先来说说第一种
先读取文件内容,然后塞进bson.binary.Binary对象里,最后像平常那样写入数据库,是不是很简单呢,获取文件一样的简单,像平时那样查找数据,然后将二进制内容写入文件即可
因为我的文件路径都带有中文,因此需要用utf-8解码,否则会报错的
使用上述方法存储小文件是很方便的,那么如果是大文件呢,可以使用gridfs
gridfs会把文件分成若干块来存储,每一块的大小默认为256K,所以,如果是小文件,就不要用gridfs来存储了,不然会浪费空间的,gridfs是MongoDB之上的分布式文件系统,可以使用mongodb的分片和复制机制,因为Mongodb分配数据空间时以2GB为单位,所以gridfs不产生磁盘碎片。
写入文件时,我们可以设置它的filename,如果多个文件使用同一个filename呢,我们在获取文件时可以使用get_version()函数,第一个参数是filename,第二个参数是版本,从0开始。
此外,我们还可以使用get(),函数,需传入文件的ObjectId
使用python操作gridfs总得来说是很方便的,毕竟所提供的函数就那么几个,稍微用心看看源码就没问题了
转自:http://www.voidcn.com/blog/KWSY2008/article/p-4871553.html
mongodb版本2.6.5
使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们先来说说第一种
先读取文件内容,然后塞进bson.binary.Binary对象里,最后像平常那样写入数据库,是不是很简单呢,获取文件一样的简单,像平时那样查找数据,然后将二进制内容写入文件即可
#coding=utf-8 ''' Created on 2015-10-8 @author: kwsy2015 ''' import pymongo import bson.binary from pymongo import MongoClient from cStringIO import StringIO def insertFile(): client = MongoClient('localhost', 27017) #获得一个database db = client.MongoFile #获得一个collection coll = db.image filename = 'F:/测试数据/hehe.jpg'.decode('utf-8') with open (filename,'rb') as myimage: content = StringIO(myimage.read()) coll.save(dict( content= bson.binary.Binary(content.getvalue()), filename = 'hehe.jpg' )) def getFile(): client = MongoClient('localhost', 27017) #获得一个database db = client.MongoFile #获得一个collection coll = db.image data = coll.find_one({'filename':'hehe.jpg'}) out = open('F:/测试数据/test4.jpg'.decode('utf-8'),'wb') out.write(data['content']) out.close() getFile()
因为我的文件路径都带有中文,因此需要用utf-8解码,否则会报错的
使用上述方法存储小文件是很方便的,那么如果是大文件呢,可以使用gridfs
gridfs会把文件分成若干块来存储,每一块的大小默认为256K,所以,如果是小文件,就不要用gridfs来存储了,不然会浪费空间的,gridfs是MongoDB之上的分布式文件系统,可以使用mongodb的分片和复制机制,因为Mongodb分配数据空间时以2GB为单位,所以gridfs不产生磁盘碎片。
#coding=utf-8 ''' Created on 2015-9-29 @author: Administrator ''' from pymongo import MongoClient from bson.objectid import ObjectId from gridfs import * def insertFile(): client = MongoClient('localhost', 27017) db = client.Pic fs = GridFS(db, 'images') with open ('F:/测试数据/hehe.jpg'.decode('utf-8'),'rb') as myimage: data=myimage.read() id = fs.put(data,filename='first') print id def getFile(): client = MongoClient('localhost', 27017) db = client.Pic fs = GridFS(db, 'images') file = fs.get_version('first', 0) data = file.read() out = open('F:/测试数据/test3.jpg'.decode('utf-8'),'wb') out.write(data) out.close() def delFile(): client = MongoClient('localhost', 27017) db = client.Pic fs = GridFS(db, 'images') fs.delete(ObjectId('560a531b0d4eae34a4edbfdd')) def listName(): client = MongoClient('localhost', 27017) db = client.Pic fs = GridFS(db, 'images') print fs.list() listName()
写入文件时,我们可以设置它的filename,如果多个文件使用同一个filename呢,我们在获取文件时可以使用get_version()函数,第一个参数是filename,第二个参数是版本,从0开始。
此外,我们还可以使用get(),函数,需传入文件的ObjectId
使用python操作gridfs总得来说是很方便的,毕竟所提供的函数就那么几个,稍微用心看看源码就没问题了
转自:http://www.voidcn.com/blog/KWSY2008/article/p-4871553.html
相关文章推荐
- python利用mongodb上传图片数据 : GridFS 与 bson两种方式
- 仿造mongodb的存储方式存一些假数据
- mongodb存储二进制数据
- 十进制浮点型数据转换成二进制,并在内存中的存储方式
- MongoDB基本概念MongoDB是一种强大灵活可扩展的数据存储方式
- 基于MongoDB GridFS取二进制数据转换为图片在jsp页面显示
- MongoDB的大数据的存储方式
- LDAP - 数据存储的一种思考方式
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法
- 打印数据在内存中的二进制存储格式
- 堆栈详解(数据与内存中的存储方式)
- Oracle分页(limit方式的运用)+付上SqlServer分页+千万级数据分页的存储过程
- 我要从com向应用程序返回一个结构,结构中包括二进制数据,请问用什么方式传替比较好呢?BSTR,VARIANT还是其他什么类型比较好呢
- C# 向数据中存储和读取二进制文件!!
- SQLite数据库如何存储和读取二进制数据
- 二进制方式存储图片(数据库)
- 以二进制方式输出数字在内存中的存储形式
- “传奇”图象数据存储方式
- 用dump函数来了解oracle的数据类型存储方式
- ofstream/ifstream 文本/二进制 方式 读入/写出 数据方法