I have followed the DigitalOcean’s tutorial using aws and also I’ve followed one using multer and multerS3,but both tutorial throw me InvalidRequest: Malformed Request
This is what I have:
const multer = require('multer')
const multerS3 = require('multer-s3')
const aws = require('aws-sdk')
const { S3_ENDPOINT, BUCKET_NAME } = process.env;
const spacesEndpoint = new aws.Endpoint(S3_ENDPOINT)
const s3 = new aws.S3({
endpoint: spacesEndpoint
})
const upload = multer({
storage: multerS3({
s3,
bucket: BUCKET_NAME,
acl: "public-read",
metadata: (req,file,cb) =>{
cb(null, {
fieldname: file.fieldname
})
},
key: (req, file, cb) =>{
console.log(file)
cb(null, file.originalname);
}
}),
}).single('upload');
module.exports={
upload,
s3
}
The server uses express, dotenv, cors, fs, the endpoint is instantiated like: app.post(‘/uploadFileRepo’, upload ,(req, res) => programCtrl.uploadFileRepo(req,res))
And uploadFileRepo only returns res.send(“Uploaded”)
And I sent the file through a Form
<form id = "repoForm"
enctype = "multipart/form-data"
action = "/uploadFileRepo"
method = "POST"
>
<div style="margin-top: 1em; margin-left: 1em;">
<input type="file" id="file" name="upload"/> <br/>
</div>
<div style="text-align: right; margin-right: 2em;">
<button type="submit" class="btn btn-outline-primary">Upload</button>
</div>
</form>
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey!
There are a few things that I could suggest starting with:
S3_ENDPOINT is the correct DigitalOcean Spaces endpoint. It should be in a format similar to https://region.digitaloceanspaces.com.https://docs.digitalocean.com/products/spaces/how-to/configure-cors/
Besides that, here are some suggestions on how to debug this further:
Console Logging: Add detailed console.log statements throughout your code:
S3_ENDPOINT and BUCKET_NAME you’re using.file in the key callback of multerS3. Check if the filename and other properties are as expected.Network Inspection: Use your browser’s developer tools (Network tab) to inspect the request being sent to DigitalOcean Spaces. Look at the following:
Content-Type, x-amz-... headers for authentication)?Simplify: Temporarily remove some complexity to isolate the problem:
aws-sdk without multer-s3 to see if the issue lies within the middleware configuration.You can also try adding error handling around your upload logic:
app.post('/uploadFileRepo', upload, (req, res) => {
if (req.file) {
programCtrl.uploadFileRepo(req, res);
} else {
res.status(400).send('Error: No file uploaded');
}
});
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.