如何用NodeJs Multer上传2个不同文件夹的图像?

ruyhziif  于 2022-12-22  发布在  Node.js
关注(0)|答案(1)|浏览(177)

我有一个问题,并坚持如何上传2图像不同的2文件夹与NodeJs Multer?我使用s3 AWS服务上传我的图像到桶
我的路线代码:

const { s3Service } = require('../../services')

module.exports = (req, res, next) => {
  const controller = new Controller()
  const router = new express.Router()

  router.route('/createEvent').post(
    s3Service.upload.single('image'),
    controller.createEvent
  )

我的Multer代码:

const multer = require('multer')
const AWS = require('aws-sdk')
const fs = require('fs')
const s3 = new AWS.S3({
  accessKeyId: process.env.BUCKET_ACCESSKEYID,
  secretAccessKey: process.env.BUCKET_SECRETACCESSKEY,
  endpoint: process.env.BUCKET_ENDPOINT
})

const storage = multer.diskStorage({
  filename: function (req, file, cb) {
    let filename = file.originalname
    filename = filename.replace(/ /g, '-')
    cb(null, 'event' + '-' + Date.now() + '-' + filename)
  }
})

const upload = multer({
  storage
})

const uploadToBucket = (fileContent, folderName, fileName, fileType) => {
  const params = {
    Bucket: process.env.BUCKET_NAME,
    Key: folderName + '/' + fileName,
    Body: fs.createReadStream(fileContent),
    ContentType: fileType,
    ACL: 'public-read'
  }

  s3.upload(params, (err, data) => {
    if (err) {
      console.log(err)
    }
    if (data) {
      fs.unlinkSync(fileContent)
      const url = data.Location
      console.log(url)

      return url
    }
  })
}

我的控制器创建函数:

const path = req.file.path
      s3Service.uploadToBucket(path, 'events', req.file.filename, req.file.mimetype)
      payload.image = {
        name: req.file.filename,
        url: process.env.BUCKET_EVENTSTORAGE + req.file.filename,
        type: req.file.mimetype,
        size: req.file.size
      }

      const data = await Event.create(payload)

我已经上传了1张图片,但我需要上传另一张名为 * 证书 * 的图片,只有当eventCertified有值 * 真 *。我坚持,从来没有尝试过。

gstyhher

gstyhher1#

使用multer,通常要处理多个文件,您只需更改

const upload = multer({
  storage
})

const max_files_limit=2 // whatever that meets your need
const upload = multer({storage}).array('images',max_files_limit); // 'images', the first parameter, should be the name of your fields in the HTML form,etc

// In your code it should be like this I suppose
s3Service.upload.array('images',max_files_limit)

您可以找到多个参照herethere作为示例。
multer中间件上传文件后,req对象将具有files属性。
您可以访问多个文件,就像这样简单

const files = req.files // simply use req.files to retreive all your files
const events_promises=[]
for(let i=0;i<req.files.length;i++){
      s3Service.uploadToBucket(files[i].path, 'events', files[i].filename, files[i].mimetype)
      payload.image = {
        name: files[i].filename,
        url: process.env.BUCKET_EVENTSTORAGE + files[i].filename,
        type: files[i].mimetype,
        size: files[i].size
      }
      events.push(Event.create(payload)) // put all your promises / async in an array
      }
      
let events_result=Promise.all(events); // wait for all your promises to finish at once
await events_result // or events_result.then("something else");

我试着遵循你的代码逻辑,但我没有测试它或模拟它,所以抱歉,如果有一些打字错误或意外错误.在我的例子中,我把所有的承诺Event. create()在一个数组中,等待所有的承诺一次,如果一个事件创建失败,在例子中的代码将无法捕捉它.
希望能有所帮助

相关问题