0

任何人都知道如何为 multer-imager dirname 字段设置自定义文件夹名称? https://www.npmjs.com/package/multer-imager

const imager = require('multer-imager');
var S3ReziseStorage = imager({
  bucket: process.env.AWS_BUCKET_NAME,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: 'us-east-1',
  filename: function (req, file, cb) {
    var folder = req.body.category ? req.body.category.toLowerCase() : '';
    var dir = 'uploads/'+ folder;
    cb(null, Date.now()+'-'+file.originalname)
  },
  dirname: dir
})


var upload = multer({ storage: S3ReziseStorage });

正如您从上面看到的,我正在尝试从 req 中提取文件夹名称,然后将其传递给 dirname。但是现在变量在文件名方法的范围内,似乎没有另一种方法来设置自定义目录名。

4

1 回答 1

0

这对我有用:

var uploadAvatar = multer({
    storage: imager({
      dirname: 'your_directory_name',
      bucket: 'your_bucket_name',
      accessKeyId: "{{ YOUR KEY-ID}}",
      secretAccessKey: "{{ YOUR SECRET}}",
      region: 'us-west-1',
      filename: function (req, file, cb) { 
        cb(null, 'subfolder/' + file.originalname )             
      },                                    
      s3 : {                                
        Metadata: {                         
          'customkey': 'data'           
        }
      }
    })
  });
于 2018-03-30T23:02:51.153 回答