파일을 MongoDB에 업로드해야합니다. 현재 Flask를 사용하여 현재 파일 시스템의 폴더에 파일을 저장하고 있습니다. GridFS를 사용하지 않고 MongoDB에 파일을 업로드 할 수있는 방법이 있습니까? 나는 오래 전에 이런 일을했다고 믿는다. 그러나 MongoDB를 사용한 이후로 오랫동안 회상 할 수는 없다.MongoDB에 파일 업로드 (크기 <16MB)
내가 업로드하도록 선택한 파일의 크기는 16MB를 넘지 않습니다.
업데이트 : binData
을 사용하여 이미지 파일을 변환하려고 시도했지만 오류 global name binData is not defined
이 표시됩니다.
import pymongo
import base64
import bson
# establish a connection to the database
connection = pymongo.MongoClient()
#get a handle to the test database
db = connection.test
file_meta = db.file_meta
file_used = "Headshot.jpg"
def main():
coll = db.sample
with open(file_used, "r") as fin:
f = fin.read()
encoded = binData(f)
coll.insert({"filename": file_used, "file": f, "description": "test" })
당신이 문서에 BLOB로 저장 시도? – Sammaye