节点js multer s3文件上传问题

时间:2020-06-12 08:24:46

标签: node.js multer-s3

我正在使用nodejs restful api并尝试使用multer在S3上上传文件,但是它不起作用,我也没有收到任何错误。

这是我的控制器中的代码

var aws = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3')
var bodyParser = require('body-parser')
var uuid = require('uuid').v4;

aws.config.update({
  secretAccessKey: '',
  accessKeyId: '' ,
  region: 'us-west-2'
});

var app = express();
var s3 = new aws.S3();

app.use(bodyParser.json());

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'stack',
    key: function (req, file, cb) {
      console.log(file);
      cb(null, req.s3key)
    }
  })
})

var fileUpload = upload.array('attachments',1);


function uploadToS3(req, res){
  req.s3key = uuid();
  let downloadUrl = 'https://s3-us-west-2.amazonaws.com/stack/'+req.s3key;
  return new Promise((resolve,reject) => {
    return fileUpload(req, res, err=> {
      if(err) return reject(err);
      return resolve(downloadUrl)
    }) 
  })
}

exports.uploadImagetoS3 = (req, res) => {
        uploadToS3(req,res).then(downloadUrl=> {
          console.log(downloadUrl);
        });
}

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

如果缺少任何内容,是否可以尝试捕获错误日志的捕获块,它将显示在日志中。

`exports.uploadImagetoS3 = (req, res) => {
    uploadToS3(req,res).then(downloadUrl=> {
      console.log(downloadUrl);
    }).catch(error=>{

console.log(错误) });` }