[英]Akka-HTTP: File Upload
I'm trying to implement a simple file upload using akka http. 我正在尝试使用akka http实现一个简单的文件上传。 My attempt looks as follows:
我的尝试看起来如下:
import akka.actor.ActorSystem
import akka.event.{LoggingAdapter, Logging}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.stream.{ActorMaterializer, Materializer}
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import scala.concurrent.{ExecutionContextExecutor, Future}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.HttpEntity
import java.io._
import akka.stream.io._
object UploadTest extends App {
implicit val system = ActorSystem()
implicit val executor = system.dispatcher
implicit val materializer = ActorMaterializer()
val config = ConfigFactory.load()
val logger = Logging(system, getClass)
val routes = {
pathSingleSlash {
(post & extractRequest) {
request => {
val source = request.entity.dataBytes
val outFile = new File("/tmp/outfile.dat")
val sink = SynchronousFileSink.create(outFile)
source.to(sink).run()
complete(HttpResponse(status = StatusCodes.OK))
}
}
}
}
Http().bindAndHandle(routes, config.getString("http.interface"), config.getInt("http.port"))
}
There are several issues with this code: 这段代码有几个问题:
Request Content-Length 24090745 exceeds the configured limit of 8388608
Request Content-Length 24090745 exceeds the configured limit of 8388608
dead letters encountered.
dead letters encountered.
exception. What is the best way to overcome the size limitations and how can I properly close the file such that a subsequent upload will overwrite the existing file (ignoring concurrent uploads for the moment)? 克服大小限制的最佳方法是什么?如何正确关闭文件,以便后续上传将覆盖现有文件(暂时忽略并发上载)?
For point 2, I think that source.to(sink).run()
executes the operation asyschronously. 对于第2点,我认为
source.to(sink).run()
异步执行操作。 It materialises a Future
. 它体现了
Future
。 Therefore your HTTP request may return before the file writing has completed, so if you start a second upload at the client as soon as the first request returns, the first one may not have finished writing to the file. 因此,您的HTTP请求可能在文件写入完成之前返回,因此如果您在第一个请求返回后立即在客户端开始第二次上载,则第一个请求可能尚未完成对文件的写入。
You could use the onComplete
or onSuccess
directive to only complete the http request when the future completes: 您可以使用
onComplete
或onSuccess
指令仅在将来完成时完成http请求:
http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/scala/http/directives/alphabetically.html http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/scala/http/directives/alphabetically.html
http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/http/routing-dsl/directives/future-directives/onSuccess.html http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/http/routing-dsl/directives/future-directives/onSuccess.html
EDIT: 编辑:
For the content length problem, one thing you can do is increase the size of that property in application.conf
. 对于内容长度问题,您可以做的一件事是在
application.conf
增加该属性的大小。 The default is: 默认为:
akka.server.parsing.max-content-length = 8m
See http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/java/http/configuration.html 见http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/java/http/configuration.html
Summarizing mattinbits comments, the follwing solution works: 总结mattinbits评论,以下解决方案的工作原理:
akka.server.parsing.max-content-length
akka.server.parsing.max-content-length
onSuccess
onSuccess
Here's a snippet of the code: 这是代码的片段:
val routes = {
pathSingleSlash {
(post & extractRequest) {
request => {
val source = request.entity.dataBytes
val outFile = new File("/tmp/outfile.dat")
val sink = SynchronousFileSink.create(outFile)
val repl = source.runWith(sink).map(x => s"Finished uploading ${x} bytes!")
onSuccess(repl) { repl =>
complete(HttpResponse(status = StatusCodes.OK, entity = repl))
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.