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

wil*_*iam 3 stream amazon-s3 vert.x

标题基本上解释了它自己。

我有一个带有VertX. 击中它后,我有一些逻辑会产生一个AWS-S3对象。

我之前的逻辑不是上传到S3,而是本地保存。所以,我可以在 response 中做到这一点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();
        });
    });
Run Code Online (Sandbox Code Playgroud)

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

谢谢

tse*_*ont 6

如果您使用 Vert.xWebClient与 S3 而不是 Amazon Java Client 进行通信,则可以完成此操作。

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
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

诀窍是使用pipe body codec