2

以下是我将文件上传到 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;
  }
};

接着:

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

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

有什么方法可以检查文件是否为zip,然后我可以解压缩并将 zip 的每个单独文件上传到 s3?我已经把我的思想缠绕在互联网上,但无法让它发挥作用。

4

0 回答 0