如何从 gridfs 读取文件到数组字节?
How to read file to array bytes from gridfs?
我想从 Enumerator[Array[Byte]] 中获取 Array/List[Byte]。一些文章向我展示了如何在 playframework 中执行此操作。但我没有为这个项目使用游戏框架。这是我的:
lazy val gridfs = GridFS[BSONSerializationPack.type](db, "resource")
gridfs.find(BSONDocument("_id" -> BSONObjectID(id))).headOption.map{
case Some(file) => {
//this gives me Enumerator[Array[Byte]].
//I'm not using playframework, how to get Future[Array[Byte]] from here?
gridfs.enumerate(file)
}
}
使用 Play Iteratees 并不特定于 Play 应用程序,而是特定于流媒体,例如 Akka Stream 或 RxStuff。
您可以使用 Iteratee.consume
.
使用此类枚举器的所有块
val sink: Iteratee[Array[Byte], Array[Byte]] = Iteratee.consume[Array[Byte]]()
val allInMem: Future[Array[Byte]] = enumerator |>>> sink
For obvious reason, it's recommanded not to consume big data, but to use appropriate Iteratee
to process the data in a streaming way.
我想从 Enumerator[Array[Byte]] 中获取 Array/List[Byte]。一些文章向我展示了如何在 playframework 中执行此操作。但我没有为这个项目使用游戏框架。这是我的:
lazy val gridfs = GridFS[BSONSerializationPack.type](db, "resource")
gridfs.find(BSONDocument("_id" -> BSONObjectID(id))).headOption.map{
case Some(file) => {
//this gives me Enumerator[Array[Byte]].
//I'm not using playframework, how to get Future[Array[Byte]] from here?
gridfs.enumerate(file)
}
}
使用 Play Iteratees 并不特定于 Play 应用程序,而是特定于流媒体,例如 Akka Stream 或 RxStuff。
您可以使用 Iteratee.consume
.
val sink: Iteratee[Array[Byte], Array[Byte]] = Iteratee.consume[Array[Byte]]()
val allInMem: Future[Array[Byte]] = enumerator |>>> sink
For obvious reason, it's recommanded not to consume big data, but to use appropriate
Iteratee
to process the data in a streaming way.