![](/img/trans.png)
[英]Akka-Streams collecting data (Source -> Flow -> Flow (collect) -> Sink)
[英]Creating a akka streams sink that sends all data to a http endpoint
我當前的代碼具有簽名:
def putObject(key: String, contentType: ContentType, data: Source[ByteString,_]): Future[HttpReponse]
(僅將其委托給akka-http客戶端)
我希望簽名是
def putObject(key: String, contentType: ContentType): Sink[ByteString, Future[HttpReponse]]
如何創建一個接收器,該接收器將所有ByteStrings用作單個HttpRequest的主體,而不在內存中緩沖所有字節?
反轉對運行流的控制是不可能的。 在第一個API中,用戶將Source賦予該庫的實現,並控制該Source何時運行。
第二個API則返回接收器。 這也意味着將運行流的控制權交還給用戶。 不支持此操作,因為現在流的某些部分需要由用戶運行,而同一流的另一部分需要通過Http庫運行。
您可以將API更改為:
def putObject(key: String, contentType: ContentType): Sink[Source[ByteString, NotUsed], Future[HttpResponse]] =
然后實現將是:
val conn = Http().outgoingConnection(host = ???)
val promise = Promise[HttpResponse]
Flow[Source[ByteString, NotUsed]]
.map(data => HttpRequest(entity = HttpEntity(contentType, data)))
.via(conn)
.toMat(Sink.head)(Keep.right)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.