Getting file contents from GridFS

345 views
Skip to first unread message

Tommi H.

unread,
Sep 1, 2014, 5:04:05 AM9/1/14
to reacti...@googlegroups.com
Hi,

(I already posted this question to Play Framework's Google Group, but perhaps it will get more attention here.)

Are there any up-to-date examples on how to use ReactiveMongo and GridFS to read file contents from MongoDB? I need to do exactly that in a Play 2.3.x project I'm working on, but I need to do it outside a controller, so I cannot use MongoController helpers of Play-ReactiveMongo plugin.

I found this surprisingly hard and documentation a bit scarce. Basically I just need to access a file, with a query, and then grab its contents somehow.

Thanks for any help in advance!

Tommi

Stephane Godbillon

unread,
Sep 4, 2014, 4:01:38 PM9/4/14
to reacti...@googlegroups.com
Hi,

You're right, there is a hole in the documentation in 0.10 regarding GridFS.
Anyway, here is an example of using gridfs without the play helpers.

import reactivemongo.api.gridfs.GridFS
import reactivemongo.api.gridfs.Implicits.DefaultReadFileReader

val gridFS = new GridFS(db)
// file selector
val query = BSONDocument("_id" -> new BSONObjectID(id))
// query files, and return a Future[Option[DefaultReadFile]]
val maybeFile = gridFS.find(query).headOption
maybeFile.map {
  case Some(file) =>
    // you can call gfs.enumerate(file) to get an Enumerator[Array[Byte]], and then consume it by applying it to an Interatee.
  case None =>
    // ...
}

Hope that it helps.

Cheers,

--
You received this message because you are subscribed to the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rajesh Bansal

unread,
Nov 10, 2014, 2:15:21 AM11/10/14
to reacti...@googlegroups.com
how to implement it for playframework 2.3.x java ,i am using this 

  String type = "image/" + NewUser.fileExtension(file.getName());
        System.out.println("type"+type);
       newUser.photo = new Blob(file, type);

but getting null pointer exception ,photo is present but still exception comes .please help me
Reply all
Reply to author
Forward
0 new messages