Lambda stream encoded string

Viewed 120

I stored the audio file in an array buffer and encoded it with base64. I need to send the data from the lambda to react client. For the larger audio files, I'm facing a lambda payload limit error.

Is there any way to stream the data in chunks from the Lambda to client ?

function readFile(filepath, callback) {
  //Uint8Array
  fs.readFile(filepath, (err, data) => {
    // Data is a Buffer object
    if (err) console.log(err);
    callback(data);
  })


readFile(`${outputFile}`, function (data) {
            try {
              let base64enc = base64.encode(data);
              responseBody.message = base64enc;
              status = statusCodes.OK;
              return sendResponse(status, responseBody);//Sending response 
            } catch (err) {
              console.log("error " + err);
              reject(err);
            }
          });
1 Answers

No, there isn't. Each Lambda function call can return one payload and Lambda function calls are independent. I suggest two possible solutions.

  1. Request a specific chunk. You can call the Lambda function multiple times with each call requesting a specific chunk of the data and merging them together as one data (file in your case) on the frontend.

  2. Use S3. You usually handle media files using S3. Assuming you already have the audio file on S3, you generate and return the pre-signed get URL object in Lambda, and use the url to get the object on the frontend. (You can refer to code on Presigned URL generation code times-out as Lambda, works locally or other sources). You can also upload audio files by getting the pre-signed put URL in Lambda and using the url on the frontend to upload.

I would suggest the second solution because it is a more standard way of dealing with media files.

Related