将S3对象流式传输到VertX Http服务器响应



标题基本上解释了自己。

我有一个带有VertX的REST端点。在命中它时,我有一些逻辑,它会产生一个AWS-S3对象。

我以前的逻辑不是上传到S3,而是在本地保存。因此,我可以在响应routerCxt.response().sendFile(file_path...)时执行此操作。

现在文件在S3中,我必须在本地下载它,然后才能调用上面的代码。

这既缓慢又低效。我想将S3对象直接流式传输到response对象。

Express中,它是这样的。s3.getObject(params).createReadStream().pipe(res);

我读了一点,发现VertX有一个名为Pump的类。但vertx.fileSystem()在实例中使用了它。

我不知道如何将InputStreamS3getObjectContent()插入vertx.fileSystem()以使用Pump

我甚至不确定Pump是正确的方式,因为我试图使用Pump返回本地文件,但它不起作用。

router.get("/api/test_download").handler(rc -> {
rc.response().setChunked(true).endHandler(endHandlr -> rc.response().end());
vertx.fileSystem().open("/Users/EmptyFiles/empty.json", new OpenOptions(), ares -> {
AsyncFile file = ares.result();
Pump pump = Pump.pump(file, rc.response());
pump.start();
});
});

有什么例子可以让我这么做吗?

感谢

如果您使用Vert.xWebClient与S3通信,而不是使用AmazonJava客户端,则可以完成此操作。

WebClient可以通过管道将内容发送到HTTP服务器响应:

webClient = WebClient.create(vertx, new WebClientOptions().setDefaultHost("s3-us-west-2.amazonaws.com"));
router.get("/api/test_download").handler(rc -> {
HttpServerResponse response = rc.response();
response.setChunked(true);
webClient.get("/my_bucket/test_download")
.as(BodyCodec.pipe(response))
.send(ar -> {
if (ar.failed()) {
rc.fail(ar.cause());
} else {
// Nothing to do the content has been sent to the client and response.end() called
}
});
});

诀窍是使用pipe主体编解码器。

最新更新