一、GridFS
GridFS将图片数据与图片属性数据分开保存,用chunks来保存图片数据,files保存属性数据,一个图片file可能对应多个chunks,每个chunk的内存大小固定(16M),若图片数据大于chunk,则分为多个chunk保存,用同一个ObjectID关联,下载时自动将多个chunk合并为图片数据。
上传
from pymongo import MongoClient
from gridfs import *
import requests
client = MongoClient('127.0.0.1', 27017) #连接mongodb
db = client.photo #连接对应数据库
#db.authenticate("username","passowd")
fs = GridFS(db, collection="images") #连接collection
data = requests.get(dic["photo_url"], timeout=10).content
if not fs.find_one({"photo_url":dic["photo_url"]}):
fs.put(data, **dic)
下载
from pymongo import MongoClient
from gridfs import *
client = MongoClient('127.0.0.1', 27017) #连接mongodb
db = client.photo #连接对应数据库
#db.authenticate("username","passowd")
fs = GridFS(db, collection="images") #连接collection
num = 1
for grid_out in fs.find(no_cursor_timeout=True):
data = grid_out.read() # 获取图片数据
outf = open('/home/%d.jpg'%num,'wb')
outf.write(data) #存储图片
outf.close()
if num%100000 == 0
metadata_file = open("/home/metadata%d.csv"%(num/100000+1), "ab")
csv_writer = csv.writer(metadata_file,delimiter='\t')
row = [grid_out.photo_title.encode('utf-8'), grid_out.uploadDate, grid_out.upload_date, \
grid_out.longitude, grid_out.latitude, grid_out.width, grid_out.height,\
grid_out.owner_name.encode('utf-8'), grid_out.photo_id, grid_out._id, grid_out.photo_url]
csv_writer.writerow(row)12345678910111213141516171819
二、bson二进制
这种方法将图片数据作为键值对放入字典与属性数据作为整体存入数据库中。
上传代码如下:
from bson import binary
from pymongo import MongoClient
client = MongoClient('127.0.0.1', 27017) #连接mongodb
db = client.photo #连接对应数据库
image_collection = db.images
data = requests.get(dic["photo_url"], timeout=10).content
if not image_collection.find_one({"photo_url":dic["photo_url"]})
dic["imagecontent"] = binary.Binary(data)
image_collection.insert(dic)1234567891011
python学习网,大量的免费MongoDB入门教程,欢迎在线学习!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。