使用python的mangodb数据库文件上传下载的代码
时间: 2023-05-24 22:06:48 浏览: 191
数据库文件上传下载代码
# 导入pymongo模块
import pymongo
from gridfs import GridFS
# 连接数据库
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client['test_database']
fs = GridFS(db)
# 上传文件
file_path = './test.txt' # 文件路径
with open(file_path, 'rb') as f:
file_id = fs.put(f, filename='test.txt')
# 下载文件
file_id = '5f34e114f33bb81cc86a5341' # 文件id
file = fs.get(file_id)
file_name = file.filename
with open(file_name, 'wb') as f:
f.write(file.read())
阅读全文