0

I have a rest api using apache camel. When I hit a post request on a route, it should get a file from S3 and send the contents of the file as a response back. I am sending json data(filename, bucketName, accesskey, secretkey, region) in order to extract the file from s3. I am able to extract the file but I don't know how to send the contents of the file back as a response. As of now, I am able to download it to my local directory.

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

Now instead of the .to("file:/tmp/") I want to send the contents of the file as a response back. How can I do this in apache camel?

Adi shukla
  • 173
  • 3
  • 11

1 Answers1

1

Camel writes the response of the component back to the response. So, if you remove the last routing to("file:/tmp/") the response will be back with the file content.

However, I don't know about the file AWS response what it will be but if it is not the one you need, you can after writing down that to the file and create a processor that reads the file and return the content. something like:

public static class HelloRoute extends RouteBuilder {
       
        @Override
        public void configure() {

        final FileReader fileReader = new FileReader();

            rest("/")
                .post("file-from-s3")
                    .route()
                    .setHeader(AWS2S3Constants.KEY, constant("filename"))
                    .to("aws2-s3://bucketnameaccessKey=INSERT&secretKey=INSERT&region=INSERT&operation=getObject")
                    .to("file:/tmp/")
                    .process(fileReader)
                    .endRest();
        }


        public class FileReader implements Processor {


            @Override
            public void process(final Exchange exchange) {
                    //read the file from "/tmp/" and write to the body
                    //exchange.getIn().setBody(....);
            }
        }
}
  • thanks, can you also pls look at this -> https://stackoverflow.com/questions/62677563/set-the-aws-s3-key-header-in-the-uri-only – Adi shukla Jul 01 '20 at 13:15
  • in this if I want to send the response in a json format with the contents of the file in the "content" key of json. How can I do this? – Adi shukla Jul 01 '20 at 21:14
  • hey can you look at this https://stackoverflow.com/questions/62861916/how-to-view-messages-from-an-sqs-queue-using-apache-camel – Adi shukla Jul 13 '20 at 12:24