I'm trying to upload files to my Digitalocean Space using an express API. The log in the below upload method never gets called, and the success status is always returned. In the route req.files the file comes through, but no file is uploaded. I can't find any information that shows a different method of that provides a better result.
My upload route:
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;
My upload function from the front end using superagent:
request
.post('http://localhost:5000/api/uploads')
.attach('upload', <image file>)
.then(() => {
console.log('done');
});