- Newest
- Most votes
- Most comments
Ok, I figured out what the issue was. You cannot pipeline the Buffer directly into the response stream. You have to wrap it in a Readable. I.e. pipeline(Readable.from(pdfBufffer), responseStream). This usage was implicitly shown in the blog post that introduces the feature but there was no mention that you must pipeline buffers in this way. This might just be a node pitfall though.
To correctly stream a Buffer containing a PDF file from a Lambda function using the streaming mode, you need to make a few adjustments to your approach. Here's how you can modify your code to achieve this:
-
Wrap your handler with
awslambda.streamifyResponseas you're already doing. -
Set the correct content type for the PDF file:
responseStream.setContentType("application/pdf");
- Instead of using
stream.pipeline, directly write the buffer to the responseStream:
const pdfBuffer = await generatePdfBuffer(); // Your existing code to generate the PDF buffer responseStream.write(pdfBuffer); responseStream.end();
-
Make sure you're not converting the buffer to a string at any point in your code. Work with the raw buffer data throughout.
-
When invoking the Lambda function, use the
InvokeWithResponseStreamCommandas you're already doing, but ensure you're handling the response correctly on the client side. -
On the client side, when processing the response, treat the payload as binary data, not as text. Instead of using a TextDecoder, you should concatenate the raw buffer chunks:
let pdfBuffer = Buffer.alloc(0); for await (const event of response.EventStream) { if (event.PayloadChunk) { pdfBuffer = Buffer.concat([pdfBuffer, event.PayloadChunk.Payload]); } }
- After collecting all chunks, you can write the complete buffer to a file or process it as needed.
The issue you're experiencing with the hex bytes being converted to decimal and then to strings is likely happening because of incorrect handling of the binary data somewhere in your process. By ensuring you're working with raw buffer data throughout and not converting to strings, you should be able to stream the PDF file efficiently.
Remember to handle any errors that might occur during the streaming process, both in the Lambda function and on the client side. Also, make sure your Lambda function has the necessary permissions to stream responses.
By following these steps, you should be able to stream your PDF buffer correctly from your Lambda function without the unwanted conversions that are causing inefficiencies in your current setup.
Sources
Writing response streaming-enabled Lambda functions - AWS Lambda
Response streaming for Lambda functions - AWS Lambda
Introducing AWS Lambda response streaming | AWS Compute Blog
Ok, I'll continue the discussion with the LLM.
- Neither the responseStream or HttpResponseStream.from have a .setContentType method
- 5-7 don't apply since I can confirm that the data is incorrect in the
event.PayloadChunk.Payload
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 2 months ago
