ジャックミャオ族:
ファイルと私はS3にアップロードして、使用してイムう私を送信するためのAPIを作成イムakka-stream-alpakka-s3
ストリームを使用してそれを行うには、ライブラリを。
私の問題は私のコントローラで私はJaveファイルにファイルを変換することができるということです。
def uploadToS3() = Action(parse.multipartFormData) { request =>
request.body.file("file").map { filePart =>
val filename = Paths.get(filePart.filename).getFileName
val file = Paths.get(s"/tmp/$filename").toFile // this is java file
saveToS3(file, filename)
}
...
}
そしてそれは「toByteArray」FUNCをしていますが、ソースのためにそれを必要とするので、私はScalaのファイルを使用しcounld funcを私のS3サービスでは、次のようになります。
import scala.reflect.io.File
class S3Service @Inject()(mys3Client: S3Client,
configuration: Configuration,
implicit val executionContext: ExecutionContext,
implicit val actorSystem: ActorSystem) {
implicit val materializer: ActorMaterializer = ActorMaterializer()
val bucket: String = configuration.get[String]("my.aws.s3.bucket")
// func to save to s3
def saveToS3(file: File, fileName: String): Future[AWSLocation] = {
// here im creating uuid so i to pass as directory so it will be possible to have files with the same name
val fileNameUUID: String = s"${UUID.randomUUID()}-$fileName"
// this will be my sinc for the stream
val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = mys3Client.multipartUpload(s"$bucket/$fileNameUUID", fileName)
// here is my issue: i need to transform the file to bytstring so I can creat it as the source but the file im getting from the controller is Java file and the function to create byteString is of Scala file so had to use scala file in this func.
Future.fromTry( Try{
ByteString(file.toByteArray())
}).flatMap { byteString =>
Source.single(byteString).runWith(s3Sink) map { res =>
AWSLocation(s"$bucket/$fileNameUUID", res.key)
}
}.recover {
case ex: S3Exception =>
logger.error("some message", ex)
throw ex
case ex: Throwable =>
logger.error("some message", ex)
throw ex
}
}
}
何が私が私のソースにバイト文字列ファイルを渡すことができるようになりますように、ファイルの種類を揃えるための最良の方法だろうか?
DVIM:
見てくださいFileIO.fromPath
、あなたを与えるだろうSource[ByteString, ...]
からとjava.nio.file.Path
。