首页 > 解决方案 > 如何使用 apache camel 以 json 格式发送从 aws s3 下载的文件的内容?

问题描述

我有一个使用 apache camel 的 rest api。当我在一条路线上发出一个发布请求时,它会从 S3 获取一个文件。这是该代码->

public static class HelloRoute extends RouteBuilder {
       
        @Override
        public void configure() {
            rest("/")
                .post("file-from-s3")
                    .route()
                    .setHeader(AWS2S3Constants.KEY, constant("filename"))
                    .to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT&region=INSERT&operation=getObject")
                    .endRest();
          }
}

这给出了 Postman 中文件的内容。我想要 json 格式的响应,其中文件的内容将在contentjson 的键中。这个怎么做?

标签: amazon-web-servicesamazon-s3apache-camel

解决方案


确保您在 REST 配置中启用了绑定模式 (auto|json) 并在您的路线上设置了消费/生产。现在编写您的处理器来构建您的响应对象并将其设置在正文中。Camel 将为您处理剩下的事情。

public static class HelloRoute extends RouteBuilder {

    restConfiguration().component("netty-http").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);
   
    @Override
    public void configure() {
        rest("/")
            .post("file-from-s3")
                .consumes("application/json").type(YourRequest.class)
                .produces("application/json").outType(YourResponse.class)
                .route()
                .setHeader(AWS2S3Constants.KEY, constant("filename"))
                .to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT&region=INSERT&operation=getObject")
                //.process("responseBuilderProcessor")
                .endRest();
      }

}


推荐阅读