我使用 Play Framework 2.6 (Scala) 和 Alpakka AWS S3 连接器将文件异步上传到 S3 存储桶。我的代码如下所示:
def richUpload(extension: String, checkFunction: (String, Option[String]) => Boolean, cannedAcl: CannedAcl, bucket: String) = userAction(parse.multipartFormData(handleFilePartAsFile)).async { implicit request =>
val s3Filename = request.user.get.id + "/" + java.util.UUID.randomUUID.toString + "." + extension
val fileOption = request.body.file("file").map {
case FilePart(key, filename, contentType, file) =>
Logger.info(s"key = ${key}, filename = ${filename}, contentType = ${contentType}, file = $file")
if(checkFunction(filename, contentType)) {
s3Service.uploadSink(s3Filename, cannedAcl, bucket).runWith(FileIO.fromPath(file.toPath))
} else {
throw new Exception("Upload failed")
}
}
fileOption match {
case Some(opt) => opt.map(o => Ok(s3Filename))
case _ => Future.successful(BadRequest("ERROR"))
}
}
它可以工作,但它会在上传到 S3 之前返回文件名。但我想在它上传到 S3 后返回值。有什么解决办法吗?
此外,是否可以将文件上传直接流式传输到 S3,以正确显示进度并且不使用临时磁盘文件?