Trouble uploading arraybuffer to AWS S3 bucket with Javascript SDK

138 Views Asked by At

Having a weird issue here and hope someone can help.

I have an app that uses Freshdesk SMI (a serverless function) to upload a voice recording to a S3 bucket. But I always get a ERR_HTTP_HEADERS_SENT error. I only get this error after I packaged the application with 'fdk pack' and then uploaded it to the Freshworks Developer portal. If I test it locally using fdk run on my computer it works.

My code looks like this:

if (options.cloudProvider === "AWS") {
        console.log("Configuring for AWS S3...");
        s3Client = new S3Client({
          region: options.iparams.recordingBucketRegionAWS,
          credentials: {
            accessKeyId: options.iparams.recordingBucketAccessKey,
            secretAccessKey: options.iparams.recordingBucketSecretKey,
          },
        });

      console.log("Fetching the file data...");
      const recordingResponse = await axios.get(options.recordingUri, {
        decompress: false,
        responseType: "arraybuffer",
      });
      const recordingFile = recordingResponse.data;

      console.log("Generating a random ID for the file key...");
      const randomId = Math.floor(Math.random() * 10000000000);
      const key = `recording-${options.ticketId}-${randomId}.mp3`;
      console.log(`Generated key for file: ${key}`);

      console.log("Sending upload command...");
      console.log("Params is: ", options.iparams.recordingBucketName);
      console.log("Params is: ", key);
      console.log("Params is: ", recordingFile);

      const uploadCommand = new PutObjectCommand({
        Bucket: options.iparams.recordingBucketName,
        Key: key,
        Body: recordingFile,
      });

      const response = await s3Client.send(uploadCommand);

      console.log("File uploaded successfully!", response);

Unfortunately I always receive the same error at the uploading to S3 part, which looks like this:

Error encountered during cloud upload: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:399:5) at ClientRequest.setHeader (node:_http_outgoing:645:11) at protocols..request (/opt/framework/index.js:53:13) at /var/task/developer/node_modules/@smithy/node-http-handler/dist-cjs/node-http-handler.js:96:25 at new Promise () at NodeHttpHandler.handle (/var/task/developer/node_modules/@smithy/node-http-handler/dist-cjs/node-http-handler.js:51:16) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /var/task/developer/node_modules/@smithy/middleware-serde/dist-cjs/deserializerMiddleware.js:5:26 { code: 'ERR_HTTP_HEADERS_SENT', '$metadata': { attempts: 1, totalRetryDelay: 0 } }

Things I have tried:

  • Using the AWS Javascript SDK to issue a S3 ListObjectsV2Command. This works and I receive the content of the bucket, so the SDK seems to work
  • Checking if I have internet access by Axios calling google
  • Converting the recordingFile from an Arraybuffer to a String and uploading that, still get the same error
  • Upgrading the AWS Javascript SDK to the latest version
  • Logging all variables/iparams/configs to see if they have the value I want, they do.

If anybody has an idea I would love to hear it and try it out. I'm at a loss for solutions at the moment. Have a good day!

0

There are 0 best solutions below