简体   繁体   中英

Downloading an mp3 file from S3 and manipulating it results in bad file

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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM