简体   繁体   中英

Node uploading mp3 to S3 file size not uploaded

I am using Node to upload mp3 files to Amazon S3. When I upload it, the file size is 9.0 Bytes and when I go to the public url, the audio file doesnt play. This is what I have,

router.post('/i', async (req, res) => {
  const fileName = 'Voice.mp3';

  fs.readFile(fileName, (err, data) => {
    if (err) throw err;

    const params = {
      Bucket: '',
      Key: '',
      Body: fileName,
      ContentType: 'audio/mpeg',
    };

    s3.upload(params, function (s3Err, data) {
      if (s3Err) throw s3Err;
      console.log(`File uploaded successfully at ${data.Location}`);
    });
  });
});

I have a local file called Voice.mp3. It uses the correct file to upload because when I change the var fileName, it gives me an error.

The fileName shows in the S3 bucket but the size and the actual file doesn't match.

I set permissions to be accessible to public as well.

The Body parameter is set to fileName , which is the name of the file, not the file contents, which I think is what you intended.

Change the code to say Body: data and it will upload the actual file contents.

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