您现在的位置是:课程教程文章
mongodb如何存储图片
2023-12-18 21:13课程教程文章 人已围观
一、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) # 上传成功后,photo数据库下出现两个collection,分别为: images.files, images.chunks12345678910111213
下载
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入门教程,欢迎在线学习!
课程教程:mongodb如何存储图片上一篇:一文了解MongoDB索引
下一篇:没有了