我想在本地上传文件,然后将该文件上传到S3。但是,穆特一次只能允许一个人

时间:2019-12-18 14:57:29

标签: node.js amazon-s3 multer pdf-extraction

我的目标是: 我想让用户上传PDF,从该PDF中提取文本,然后将文本分配给数组对象。完成此操作后,我想将该文件上传到S3存储桶。现在,我可以毫无困难地完成第一部分。首先要进行本地上传的原因是,我可以从PDF中提取文本。这些方法可以独立工作。如果要上传到S3,它将在数据库中填充链接,以便我在前端显示它,但BulletinMetaText字段未填充提取文本。

我应该怎么做?

如果我不清楚,请告诉我我还能提供什么。

多重方法:

let uploadToS3 = multer({
  storage: multerS3({
    s3: s3,
    acl: "private",
    bucket: env == "production" ? "xxxx" : "xxxx",
    // metadata: function(req, file, cb) {
    //   console.log(file);
    //   console.log(req.body);
    //   cb(null, Object.assign({}, req.body));
    // },
    key: function(req, file, cb) {
      // console.log(file);
      cb(null, `${new Date().getFullYear()}/${file.originalname}`);
    }
  })
}).array("files");
// }).any();

var tempStorage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, "client/v1.7/src/assets/pdf/");
  },

  // By default, multer removes file extensions so let's add them back
  filename: function(req, file, cb) {
    cb(null, `${file.originalname}`);
  }
});

var uploadToLocal = multer({ storage: tempStorage }).array("files");

let delay = time => {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
      resolve("DONE");
    }, time);
  });
};

节点/表示后端

if (req.params.type === "files") {
    uploadToLocal(req, res, function(err) {
      if (err) {
        next({
          status: 500, //Server Error
          statusMessage: "Error: Failed to save file ",
          catchError: err //System error
        });
      } else {
        //TODO

        let {
          bulletinID,
          bulletinUUID,
          bulletinActive,
          bulletinType,
          bulletinCode,
          bulletinGroup,
          bulletinEn,
          bulletinFr,
          bulletinTitleEn,
          bulletinDescriptionEn,
          bulletinTitleFr,
          bulletinDescriptionFr,
          metaStringTags,
          bulletinPermission
        } = req.body.model;

        uploadToS3(req, res, function(err) {
          console.log("upload2Req");
          console.log(req.body);

          let bulletinFileEn,
            bulletinFileFr = "";

          // console.log("req.files");
          console.log(req.files);

          req.files.forEach(file => {
            if (file.originalname && file.originalname.includes("_en")) {
              console.log("file");
              console.log(file);
              console.log(file.key);

              bulletinFileEn = file.key;
            }
            if (file.originalname && file.originalname.includes("_fr")) {
              bulletinFileFr = file.key;
            }
          });
          console.log(bulletinFileEn);
          console.log(bulletinFileFr);

          extract(
            "client/v1.7/src/assets/pdf/test.pdf",
            // req.files.originalname,
            { splitPages: false },
            function(err, text) {
              if (err) {
                console.log(err);
                return;
              }
              console.log("text");
              // console.log(text);
              bulletinMetaText = text;

              if (err) {
                next({
                  status: 500, //Server Error
                  statusMessage: "Error: Failed to save file locally ",
                  catchError: err //System error
                });
              } else {
                // !~

                let newBulletin = {
                  bulletinID: bulletinID ? bulletinID : "NULL",
                  bulletinUUID: bulletinUUID ? bulletinUUID : uuid(),
                  bulletinActive: bulletinActive ? bulletinActive : true,
                  postedByUserID: req.user.id,
                  postedByUserUUID: req.user.userUUID,
                  bulletinType: bulletinType,
                  bulletinCode: bulletinCode,
                  bulletinGroup: bulletinGroup,
                  bulletinEn: bulletinEn ? true : false,
                  bulletinFr: bulletinFr ? true : false,
                  //English Bulletin - File/Link
                  bulletinTitleEn: bulletinTitleEn,
                  bulletinDescriptionEn: bulletinDescriptionEn,
                  bulletinLinkEn: null,
                  bulletinFileEn: bulletinFileEn,
                  //French Bulletin - File/Link
                  bulletinTitleFr: bulletinTitleFr,
                  bulletinDescriptionFr: bulletinDescriptionFr,
                  bulletinLinkFr: null,
                  bulletinFileFr: bulletinFileFr,
                  metaStringTags: metaStringTags,
                  bulletinPermission: bulletinPermission,
                  bulletinTextScrape: bulletinMetaText
                };

                console.log(newBulletin);
                InsertOrUpdateBulletin(newBulletin, req.user)
                  .then(result => {
                    res.status(200).json({
                      data: result,
                      status: 200, //Created
                      statusMessage: "Success: Bulletin Created"
                    });
                  })
                  .catch(error => {
                    next({
                      status: 500, //Server Error
                      statusMessage: "Error: Failed to save Bulletin ",
                      catchError: error //System error
                    });
                  });
              }
            }
          );
        });
      }
    });
  } 

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助(关键字:可能),但它使用express-fileupload。仍然允许服务器端对数据进行操作。

https://link.medium.com/U1SdsoHMy2