Not able to stream PDF using AWS gateway Lambda setup

0 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

Note:- I've a workaround in place, where I create a signedURL to S3 object, and send it as redirect. However, I am trying to make it work by streaming a binary file using Lambda.

Thanks for reading such a long post. Any pointers, links in the right direction are highly appreciated.

Jul 31, 2018 in AWS by bug_seeker
• 15,510 points
3,456 views

1 answer to this question.

0 votes

I 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 Jul 31, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer

Not able to get_item from AWS dynamodb using python?

You are mixing resource and client objects ...READ MORE

answered Feb 24, 2022 in AWS by Korak
• 5,820 points
1,735 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers
+2 votes
3 answers
+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,761 views
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