2015-02-04 5 views
0

사용자가 이미지를 업로드 할 때 PyMongo을 사용하여 데이터베이스에 연결하고 있습니다. 컬렉션의 사용자 특정 문서 내에 GridFS을 사용하여 이미지를 저장하고 싶습니다.이미지를 MongoDB에 저장하십시오.

class uploadHandler(BaseHandler): 

@tornado.web.authenticated 

def get(self): 

    self.render("upload.html",user=self.current_user) 

def post(self): 
    db = pymongo.Connection('mongodb://heroku_app.mongolab.com:/heroku_app').heroku_appxxxx 
    user = db.userInfo.find_one({'Username':self.current_user}) 
    file1 = self.request.files['images'][0] 
    original_fname = file1['filename'] 

    print "file: " + original_fname + " is uploaded" 

    fs = gridfs.GridFS(user) 
    fs.put(file1) 
    #db.userInfo.update({'Username':self.current_user},{'$push':{'Images': file1}}) 

    self.redirect('/upload') 

을하지만이 나에게 오류를 제공합니다 : I는 그렇게하고 있어요 이미지가 가장 좋은 다음 집합에서 특정 문서의 MongoDB를 저장 할 수있는 방법

Traceback (most recent call last): 
    File "C:\Python27\lib\site-packages\tornado\web.py", line 1332, in _execute 
    result = method(*self.path_args, **self.path_kwargs) 
    File "C:\Users\rsaxs\workspace\pie\src\Server.py", line 4101, in post 
    fs = gridfs.GridFS(user) 
    File "C:\Python27\lib\site-packages\gridfs\__init__.py", line 53, in __init__ 
    raise TypeError("database must be an instance of Database") 
TypeError: database must be an instance of Database 

?

+0

그래, 추적 라인의 마지막 라인은 * 믿어지지 않는 * 도움이된다. – vaultah

+0

@vaultah, 더 도움이되도록 업데이트되었습니다. – user94628

답변

2

저장하고 BSON 문서 파일 크기 제한 (16MB의)를 초과 파일을 읽을 수 있습니다. 데이터를 클라이언트에 스트리밍 할 수 있으므로 미디어를 저장하는 데 자주 사용됩니다.

GridFS는 실제로 여러 문서로 구성되어 있습니다. GridFS에 무언가를 삽입하면 db에서 확인하십시오. 특수 구조를 가지고 있으며 files이 잠재적으로 16MB 문서 크기 제한을 초과 할 수 있으므로 GridFS 문서를 다른 문서에 포함 할 수 없습니다.

일부 GridFS 개체를 참조하려는 경우 특정 GridFS 파일의 file_id을 저장하고 해당 콘텐츠를 검색해야 할 때 쿼리 할 수 ​​있습니다.

1

데이터베이스 개체를 GridFS 생성자로 전달해야합니다. GridFS

fs = gridfs.GridFS(db) 
+0

파일을 컬렉션의 특정 문서에 삽입하려면 어떻게해야합니까? – user94628

+0

@ user94628 그럼 GridFS가 아닙니다. – Pio

+0

@Pio, ok 고맙습니다. 업데이트 된 질문을 통해 이미지가 특정 문서에 저장되는 방식을 확인할 수 있습니다. – user94628

관련 문제