node.jstypescriptamazon-web-servicesamazon-s3multer

Multer s3 upload: File in s3 not playing and file increase


I am uploading a mp4 file to s3 via node.js s3.

The code works and I see the file in the s3. However The original file is 860kb but when uploading it it turns to 1.4MB and when I download it from S3 it never plays.

Code:

//setup

const storageS3: StorageEngine = multer.memoryStorage();
const uploadS3 = multer({ storage: storageS3 });

//Upload

const s3Upload = async (req: Request, res: Response, next: NextFunction) => {
  const files = req.files as Express.Multer.File[];
  const uploadPromises = files.map(async (file) => {

    const params = {
      Bucket: process.env.S3_BUCKET_NAME!,
      Key: `uploads/${file.originalname}`, 
      Body: file.buffer,
      ContentType: "video/mp4", // for test purposes
    };

    return s3.upload(params).promise();
  });

  try {
    await Promise.all(uploadPromises);
    next();
  } catch (err) {
    next(err);
  }
};

//Route middlware

const determineUploadHandler = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  if (isLocal) {
    upload.array("media")(req, res, next);
  } else {
    logFileSizeBeforeMulter(req, res, () => {
      uploadS3.array("media")(req, res, async (err) => {
        if (err) return next(err);
        await s3Upload(req, res, next);
      });
    });
  }
};

Route:

export const uploadHandler = router.post(
  "/",
  determineUploadHandler,
  (req: Request, res: Response) => {
    res.status(200).json({ message: "Files uploaded successfully" });
  }
);

File:

enter image description here

In s3:

enter image description here

I download the file and the video never starts. What have I done wrong?


Solution

  • So i found the issue.

    There was an issue with getting the double size and it seems the issue was in the APIGateway.

    I added this:

      apiGateway:
        binaryMediaTypes:
          - "multipart/form-data"
    

    In my serverless file and now it works.

    Detailed answer here