4

Here's how I'm uploading files to AWS S3:

const config = (req) => {
  limits: { fileSize: 20000000 }, // 20 MB
  storage: multerS3({
    acl: 'public-read',
    bucket: `${Bucket}/${orderNumber}/orderFiles`,
    key(request, file, cb) {
      cb(null, `${file.originalname}`);
    },
    metadata(request, file, cb) {
      cb(null, {});
    },
    s3,
  }),
}

uploadFiles = async req => {
  try {
    const uploadOrder = multer(config(req)).array('files');

    return new Promise((resolve, reject) => {
      uploadOrder(req, {}, error => {
        const fileArray = req.files;
        let fileLocation;

        fileArray.forEach(file => {
          fileLocation = file.location;
        });

        resolve(fileArray[0].bucket);
      });
    });
  } catch (error) {
    return error;
  }
};

And then:

router.post('/addFiles', auth.admin, async (req, res, next) => {
  try {
    await uploadFiles(req);
    // handle rest of the things...

    res.send();
  } catch (error) {
    next(error);
  }
});

Is there any way I can check if the file is zip and then I can unzip and upload each individual file of the zip to s3? I have wrapped my mind around the internet and could not get it working.

Jeff Adam
  • 103
  • 1
  • 11

0 Answers0