将S3对象流式传输到VertX Http Server响应 [英] Streaming S3 object to VertX Http Server Response

查看:89
本文介绍了将S3对象流式传输到VertX Http Server响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题基本上说明了自己.

The title basically explains itself.

我有一个 VertX 的REST端点.碰到它时,我有一些逻辑结果会导致生成一个 AWS-S3 对象.

I have a REST endpoint with VertX. Upon hitting it, I have some logic which results in an AWS-S3 object.

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

My previous logic was not to upload to S3, but to save it locally. So, I can do this at the response routerCxt.response().sendFile(file_path...).

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

Now that the file is in S3, I have to download it locally before I could call the above code.

那是缓慢而低效的.我想将S3对象直接流式传输到 response 对象.

That is slow and inefficient. I would like to stream S3 object directly to the response object.

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

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

I read a little bit, and saw that VertX has a class called Pump. But it is used by vertx.fileSystem() in the examples.

我不确定如何将 InputStream S3 getObjectContent()插入到 vertx.fileSystem()使用 Pump .

I am not sure how to plug the InputStream from S3'sgetObjectContent() to the vertx.fileSystem() to use Pump.

我什至不确定 Pump 是正确的方法,因为我尝试使用 Pump 返回本地文件,但是它没有用.

I am not even sure Pump is the correct way because I tried to use Pump to return a local file, and it didn't work.

    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();
        });
    });

请问有什么例子可以做到吗?

Is there any example for me to do that?

谢谢

推荐答案

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

It can be done if you use the Vert.x WebClient to communicate with S3 instead of the Amazon Java Client.

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

The WebClient can pipe the content to the HTTP server response:

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 body编解码器.

这篇关于将S3对象流式传输到VertX Http Server响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆