1

I did a script that downloads a MP3 file from my S3 bucket and then manipulates in before download (Adding ID3 Tags).

It's working and the tags are injected properly, but the files corrupts as it seems and unplayable. I still can see my tags trough MP3tag so it has data in it, but no audio is playing trough the file.

Heres my code, Trying to figure it what went wrong

 const downloadFileWithID3 = async (filename, downloadName, injectedEmail) => {
  try {
    const data = await s3Client.send(
      new GetObjectCommand({
        Bucket: "BUCKETNAME",
        Key: filename,
      })
    );

    const fileStream = streamSaver.createWriteStream(downloadName);
    const writer = fileStream.getWriter();
    const reader = data.Body.getReader();

    const pump = () =>
      reader.read().then(({ value, done }) => {
        if (done) writer.close();
        else {
          const arrayBuffer = value;
          const writerID3 = new browserId3Writer(arrayBuffer);
          const titleAndArtist = downloadName.split("-");
          const [artist, title] = titleAndArtist;

          writerID3.setFrame("TIT2", title.slice(0, -4));
          writerID3.setFrame("TPE1", [artist]);
          writerID3.setFrame("TCOM", [injectedEmail]);
          writerID3.addTag();
          let taggedFile = new Uint8Array(writerID3.arrayBuffer);

          writer.write(taggedFile).then(pump);
        }
      });

    await pump()
      .then(() => console.log("Closed the stream, Done writing"))
      .catch((err) => console.log(err));
  } catch (err) {
    console.log(err);
  }
};

Hope you can help me solve this wierd bug, Thanks in advance!

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • 1
    Are you sure the problem has to do something with S3? Does the same code work if you replace the reading file from S3 with the reading file from a local filesystem? – Vlad Holubiev Sep 18 '22 at 14:16
  • When I just download regularly without the manipulation of the ID3 it's all good. when I download with the manipulation its downloading also, but the file corrupts and is unplayable but I can still read the ID3 tags from it. i think it has to do with the Uint8Array but I dont seem to find a way to fix it. – Menash Bouhadana Sep 18 '22 at 15:13

1 Answers1

0

Ok so i've figured it out, instead of using chunks of the stream itself i've used getSignedUrl from the s3 bucket it works.

Thanks everyone for trying to help out!