@Pymongo GridFS
利用pymongo存储大空间文件
pymongo执行插入数据库报错、选用Gridfs插入大于16MB文件,查阅材料并记录如下。
GridFS
GridFS 使用两个集合(collection)存储文件。一个集合是 chunks ,用于存储文件内容的二进制数据;一个集合是 files,用于存储文件的元数据
Please see 下面 code
from pymongo import MongoClient
from gridfs import *
import json
import os
class Grid:
def __init__(self, db_name=None, collection=None, host="localhost", port=27017):
self.db_name = db_name
self.collections = collection
self.host = host
self.port = port
def insert_files(self, file):
client = MongoClient(self.host, self.port)
db = client[self.db_name]
fs = GridFS(db, self.collections)
with open(file, "rb") as f:
data = f.read()
# 自定义文件名字类型
filename = "{}".format(file.split("\\")[-1])
id = fs.put(data=data, filename=filename)
print("id:", id, "Done")
def get_files(self, file):
client = MongoClient(self.host, self.port)
db = client[self.db_name]
fs = GridFS(db, self.collections)
file = fs.get_version(filename=file, version=0)
data = file.read()
data = json.loads(str(data, encoding="utf8"))
return data
if __name__ == '__main__':
gd = Grid(db_name="GUANGDONG", collection="indicator7")
result = gd.get_files("ind7-2020-06-30.json")
print(result)