下面是一个简单的示例,它获取文件路径列表,将列表转换为单个
Source[ByteString, _]
,并运行
Source
使用
Alpakka S3 connector
Sink
将数据上传到S3:
val paths = List(Paths.get("/path/to/file1"), Paths.get("/path/to/file2"))
val source: Source[ByteString, _] = Source(paths).flatMapConcat(FileIO.fromPath(_))
// read the Alpakka documentation about setting up a S3 client and sink
val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = ???
val fut: Future[MultipartUploadResult] = source.runWith(s3Sink)
你可以使用
fut
其中一个
future directives
在Akka HTTP路由中。
如上所述,上述方法创建了一个
来源
。如果每个文件需要不同的存储桶和密钥,则可以为每个文件启动单独的流:
val source1: Source[ByteString, _] = FileIO.fromPath(Paths.get("/path/to/file1"))
val source2: Source[ByteString, _] = FileIO.fromPath(Paths.get("/path/to/file2")
val s3Sink1: Sink[ByteString, Future[MultipartUploadResult]] = ???
val s3Sink2: Sink[ByteString, Future[MultipartUploadResult]] = ???
val fut1: Future[MultipartUploadResult] = source1.runWith(s3Sink1)
val fut2: Future[MultipartUploadResult] = source2.runWith(s3Sink2)
val fut: Future[List[MultipartUploadResult]] = Future.sequence(List(fut1, fut2))