I am not able to stream PDF using AWS gateway Lambda setup

+2 votes

Problem Statement:- Not able to stream PDF, using AWS gateway/Lambda setup.

I've following Setup:-

Gateway API -> Lambda Function(java) -> S3

Api should stream a PDF back to the client via Lambda from S3 server. I am unable to do so.

Things tried

  • 1) On Lambda side
    • Use RequestStreamHandler, write PDF on OutputStream
      outputStream.write(); 
      • Tried Setting Base64 encoding using following
        • AWS Base64 Utility
        • java.util Base64 Utility
        • apache Base64 utility
      • Send Byte[] without encoding
    • Use non-proxy handler, write PDF as string to one of the output variable.
      • Tried Setting Base64 encoding using following
        • AWS Base64 Utility
        • java.util Base64 Utility
        • apache Base64 utility
      • Send Byte[] without encoding
  • 2) On Gateway Side
    • Set Proxy integration
    • Remove Proxy integration
    • Tried Set Content Handling using
      • Passthrough
      • Convert to Binary
      • Convert to String
      • Set headers
        • Content-Type = 'application/pdf'
        • Content-Disposition = 'attachment; filename="nameofpdffile.pdf"'

I've tried all the permutations/combinations of these 2. However, I am not able to get it to work and documentation around this area seems poor.

When I set Base64 encoding in Lambda and on gateway side specify content handling as convert to binary, i get error, fail to conert using Base64 decode. For other combinations, I get binary data as output when I test it using gateway Test funciton, however my client(Postman, Chrome, Safari) fail to convert it to PDF

Kindly help me out in this case.

Apr 24, 2018 in AWS by Flying geek
• 3,280 points
7,805 views

3 answers to this question.

0 votes

 had a similar problem with trying to offer up gzipped content: Client -> API-Gateway -> S3.

I couldn't figure out why the content I pulled couldn't get recognized by the client (either browser or code) until I inspected the header(the binary header, not http). It appears that API Gateway by default assumes a string based encoding (utf-8) so what I was seeing is data that API Gateway was transforming on the fly and adding utf-8 headers in various places.

Late last year API Gateway started to support binary payloads. I started reading this article which may help in your case. It did not in mine, but your use case is slightly different and closer to the article so it might.

https://aws.amazon.com/blogs/compute/binary-support-for-api-integrations-with-amazon-api-gateway/

As for what I ended up doing. I realized I was just using the API Gateway as a passthrough (to take advantage of the Cognito based authentication I had already setup) so I bypassed API Gateway all together and used the aws js sdk to connect directly to s3 (and set IAM policies on the bucket separately)

answered Apr 24, 2018 by Cloud gunner
• 4,670 points
+1 vote
I got a discussion on aws forum hope this helps you

https://forums.aws.amazon.com/thread.jspa?threadID=195218
answered Aug 2, 2018 by bug_seeker
• 15,510 points
0 votes

Lambda can be used to return binary data through the Java programming model. See the input/output stream example given here: http://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html

I was able to invoke this from the CLI and get
binary output as desired however when defining an API Endpoint to invoke the function the results was distorted. I'm guessing the API Gateway interprets the Lambda response as a string?

answered Aug 22, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

+1 vote
3 answers

Not able to pass params POST to AWS Lambda from Amazon API Gateway

For this template : application/x-www-form-urlencoded  This line below will ...READ MORE

answered Jun 13, 2018 in AWS by Cloud gunner
• 4,670 points
8,769 views
0 votes
1 answer
0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24, 2020 in AWS by MD
• 95,460 points
2,470 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+3 votes
3 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP