我正在尝试使用快速 API 将文件上传到我的 Digitalocean Space。以下上传方法中的日志永远不会被调用,并且总是返回成功状态。在路线 req.files 文件通过,但没有文件上传。我找不到任何信息显示可以提供更好结果的不同方法。
我的上传路线:
const express = require('express');
const AWS = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const api = express.Router();
AWS.config.update({
accessKeyId: process.env.SPACES_ACCESS_KEY_ID,
secretAccessKey: process.env.SPACES_ACCESS_KEY,
});
const spacesEndpoint = new AWS.Endpoint('nyc3.digitaloceanspaces.com/uploads');
const s3 = new AWS.S3({
endpoint: spacesEndpoint,
});
const upload = multer({
storage: multerS3({
s3,
bucket: '<my bucket>',
acl: 'public-read',
// eslint-disable-next-line
key: function (req, file, cb) {
console.log(file);
cb(null, file.originalname);
},
}),
}).single('upload');
api.post('/', (req, res, next) => {
console.log(req.files);
upload(req, res, err => {
if (err) {
console.log(err);
return res.status(400).json({ errors: err });
}
return res.status(200).json({ message: 'File uploaded successfully.' });
});
});
module.exports = api;
我使用超级代理从前端上传功能:
request
.post('http://localhost:5000/api/uploads')
.attach('upload', <image file>)
.then(() => {
console.log('done');
});