Storing and retrieving images in MongoDB

983 views
Skip to first unread message

bg

unread,
Mar 1, 2013, 2:01:25 PM3/1/13
to lif...@googlegroups.com
Hi,

For Mongo - most suggestions are to use GridFS
I've found the mongo Java docs about the driver support

So would like to check  the correct way to use this in my model classes
Do I add a binary field to a MongoRecord class? 
Or does GridFS get handled separately - do I just work directly with the Java driver?

Thanks

Antonio Salazar Cardozo

unread,
Mar 2, 2013, 11:48:05 AM3/2/13
to lif...@googlegroups.com
My advice is somewhat unrelated to GridFS, which is: use Amazon S3 or a similar service. In general, it's easier to manage, and if you start accumulating files/data, it will be even more so.

That said, if memory serves from when we were using it, GridFS is handled separately, and I don't know that there is a Record adapter for GridFS stuff as a result.
Thanks,
Antonio

Tim Nelson

unread,
Mar 2, 2013, 5:46:11 PM3/2/13
to lif...@googlegroups.com
Hi bg,

There isn't anything build int to lift-mongo-record, but there are some things that can help and using GridFS is really simple:


Tim

Lukasz Kuczera

unread,
Mar 3, 2013, 1:48:36 PM3/3/13
to lif...@googlegroups.com
If your images will be small < 16MB you can store them as byte arrays inside mongo documents.

bg

unread,
Mar 4, 2013, 1:47:59 PM3/4/13
to lif...@googlegroups.com
hi,

As my images are small, I've tried adding "object image extends BinaryField(this)" to my model, using https://github.com/eltimn/binaryfield-demo as a base. I think my set is working, but my get doesn't seem to return the data.


set:

def uploadImage(req: Req): Box[LiftResponse] = {
    for (file <- req.uploadedFiles) {
      println("Received: " + file.fileName)

      EventMeta.findByName("BillsBack").map(_.image.set(file.file))
    }
    Full(OkResponse())
  }

After uploading a file - I can read it in the mongo CLI

db.events.find()[0].image
BinData(0,"AA==") <= not sure if that is the trunced binary data of the file I uploaded


I'm doing the get as

  def getImage(req: Req): Box[LiftResponse] = {
    val event = EventMeta.findByName("BillsBack")
    // val data: Array[Byte] = event map (_.image.is) getOrElse (Array.empty)
    val data: Array[Byte] = event match {
      case Some(aEvent) => aEvent.image.is match {

        case daBinary: Array[Byte] => daBinary
        case _ => Array.empty
      }

      case _ => Array.empty
    }

    Full(InMemoryResponse(data, Nil, Nil, 200))

  }


When I debug the get , it does break on the "daBinary"; however it is an array with a value of "[0] = 0"

Cheers

Alexandre Russel

unread,
Mar 4, 2013, 11:09:52 PM3/4/13
to lif...@googlegroups.com
On Mon, Mar 4, 2013 at 7:47 PM, bg <brent...@gmail.com> wrote:
> hi,
>
> As my images are small, I've tried adding "object image extends
> BinaryField(this)" to my model, using
> https://github.com/eltimn/binaryfield-demo as a base. I think my set is
> working, but my get doesn't seem to return the data.
>
>
> set:
>
> def uploadImage(req: Req): Box[LiftResponse] = {
> for (file <- req.uploadedFiles) {
> println("Received: " + file.fileName)
>
> EventMeta.findByName("BillsBack").map(_.image.set(file.file))

can you try= EventMeta.findByName("BillsBack").map(_.image.set(file.file)).save

> }
> Full(OkResponse())
> }
>
> After uploading a file - I can read it in the mongo CLI
>
> db.events.find()[0].image
> BinData(0,"AA==") <= not sure if that is the trunced binary data of the file

no, this looks more like an empty field
> --
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you:
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to liftweb+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

bg

unread,
Mar 5, 2013, 3:58:50 AM3/5/13
to lif...@googlegroups.com
oh yes, saving - that helps! thanks for spotting my rookie error.

Cheers
Reply all
Reply to author
Forward
0 new messages