Search code examples
javascriptnode.jsexpressamazon-s3aws-sdk-nodejs

AWS mp3 file upload is failing intermittently


I m facing issue with mp3 file uploads to S3 bucket using express-fileupload. For most of the users, it works fine but for some users, the md5 doesnt matches while uploading so the upload fails. I m using following code to upload the file.

export const uploadFileToS3Bucket = async (fileToUpload, fileId, subKey) => {
  const fileExtension = fileToUpload.name.split(".");

  const s3Bucket = new AWS.S3({
    accessKeyId: AppConstants.S3_UPLOAD_ACCESS_ID,
    secretAccessKey: AppConstants.S3_UPLOAD_ACCESS_KEY,
    region: AppConstants.S3_REGION,
  });

  const key = `${subKey}/${fileId}.${fileExtension[fileExtension.length - 1]}`;

  const params = {
    Bucket: BucketName,
    Key: key,
    Body: fileToUpload.data,
    ContentType: fileToUpload.mimetype,
    ACL: "public-read",
    ContentMD5: fileToUpload.md5,
  };

  return new Promise((resolve, reject) => {
    s3Bucket.upload(params, (err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data.Location);
      }
    });
  });
};

Solution

  • We found an issue were we were not maintaining user stickiness in between our deployed PM2 instances. So if we sent the request from instance 1, the response had a chance to be received by the instance 2 cause the issue in the upload. We enabled user stickiness and this fixed the issue.