我正尝试使用Play的EssentialAction
以下列方式将文件内容流式传输到S3:
def upload(fileName: String) = EssentialAction { request =>
val (iteratee, enumerator) = Concurrent.joined[Array[Byte]]
blobStore.streamToStore(fileName, enumerator) //consumes the enumerator, streams to S3 and returns Future[Unit]
iteratee.map { _ =>
Logger.debug(s"Successfully uploaded file=$fileName")
Created(Json.obj("id" -> fileName))
}
}
如果blobStore.streamToStore
流成功,但如果流式传输时出现任何错误,则上述工作正常,但请求永远不会完成,在recover
上执行iteratee
也无济于事。在这种情况下,有人可以解释如何处理错误吗?
答案 0 :(得分:0)
我必须在错误时完成使用枚举器:
blobStore.streamToStore(fileName, enumerator).recover {
case throwable =>
/* consume the enumerator for request to complete */
enumerator |>> Iteratee.consume[Array[Byte]]().map(_ => throw throwable)
}
iteratee.map { _ =>
Logger.debug(s"Successfully uploaded file=$fileName")
Created(Json.obj("id" -> fileName))
}.recover {
case NonFatal(throwable) =>
InternalServerError(Results.EmptyContent())
}