Problem uploading a arraybuffer to S3

0

Hey all! Im trying to upload a voice recording from my app to an S3 bucket. The code does the following:

  1. Downloads the voice recording with axios
  2. Generate a random key
  3. Uploads the recording to an AWS S3 bucket
// Depending on the cloudProvider, set up the S3 client configuration
      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, {
        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}`);


      // try {
      //   let resint = await axios.get('http://www.google.com');
      //   console.log("Internet access is available.", resint);
      // } catch (error) {
      //   console.error("No internet access:", error);
      // }

      // try {
      //   const s3Client = new S3Client({
      //     region: options.iparams.recordingBucketRegionAWS,
      //     credentials: {
      //       accessKeyId: options.iparams.recordingBucketAccessKey,
      //       secretAccessKey: options.iparams.recordingBucketSecretKey,
      //     },
      //   });
      //  let resaws = await s3Client.send(new ListObjectsV2Command({ Bucket: options.iparams.recordingBucketName }));
      //   console.log("AWS S3 access is available.", resaws);
      // } catch (error) {
      //   console.error("Unable to access AWS S3:", error);
      // }

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

      const response = await s3Client.send(uploadCommand);

When trying to upload the file to the bucket, it throws the following error:

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.<computed>.request (/opt/framework/index.js:53:13) at /var/task/developer/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http-handler.js:72:25 at new Promise (<anonymous>) at NodeHttpHandler.handle (/var/task/developer/node_modules/@aws-sdk/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/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:5:26 { code: 'ERR_HTTP_HEADERS_SENT', '$metadata': { attempts: 1, totalRetryDelay: 0 } } 

Thing I have already tried:

  1. Double checking if all the variables and options have the correct values. This is true
  2. See if I have AWS / internet access by running the commented code and accessing google / list buckets. Both these commands work!

Does anybody have a suggestion on how to fix this? Would be appreciated.

DJRita
gefragt vor 9 Monaten78 Aufrufe
Keine Antworten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen