1

I'm using PyMongo to connect to my database, when a user uploads an image I want to store it using GridFS within the users specific document in the collection. I'm doing it as so:

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')

But this gives me the error:

  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

How can an image be best stored in a mongoDB in a particular document within a collection then?

user94628
  • 3,641
  • 17
  • 51
  • 88

2 Answers2

2

With GridFS you can save and read files that exceed the BSON document file size limit (16 MB). It is often used to store media since you can stream your data to the client.

GridFS has a specific document structure -- it actually consists of multiple documents, just check it in your db when you insert something to GridFS. Since it has a special structure and files can potentially exceed the 16MB document size limit you cannot include GridFS docs in other documents.

If you want to reference some GridFS objects you can by saving the file_id of the specific GridFS file and querying that when you need to retrieve it's content.

Pio
  • 4,044
  • 11
  • 46
  • 81
1

You need to pass in the database object into the GridFS constructor.

fs = gridfs.GridFS(db)
huderlem
  • 251
  • 1
  • 5
  • What if you want to insert the file into a particular document in the collection? – user94628 Feb 04 '15 at 17:54
  • @user94628 That's not GridFS then. – Pio Feb 04 '15 at 17:56
  • @Pio, ok thanks, updated question to see how images can be saved in a specific document then. – user94628 Feb 04 '15 at 18:03
  • @user94628 Then please read what [GridFs](http://docs.mongodb.org/manual/core/gridfs/) is used for. The idea is that it will break your data into chunks and serve them. It is a special kind of document (actually a set of documents) that cannot be included/cannot include other ordinary documents. – Pio Feb 04 '15 at 18:10
  • @Pio then to retrieve them for the specific user would I include the `files_id` as a record in the users document? – user94628 Feb 04 '15 at 18:14