Report this

What is the reason for this report?

AWS-SDK returns me InvalidRequest: Malformed Request

Posted on August 17, 2020

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

There are a few things that I could suggest starting with:

  • Incorrect Endpoint: Double-check that your S3_ENDPOINT is the correct DigitalOcean Spaces endpoint. It should be in a format similar to https://region.digitaloceanspaces.com.
  • Region Mismatch: Ensure the region in your endpoint matches the region where your Space is located.
  • Authentication Issues: Verify that your Spaces credentials have the necessary permissions to write to your DigitalOcean Space.
  • CORS Configuration: If you’re working with a web frontend, make sure your DigitalOcean Space has a CORS policy allowing requests from your frontend’s origin:

    https://docs.digitalocean.com/products/spaces/how-to/configure-cors/

  • Filename and Encoding: Confirm the filename is valid (avoiding special characters) and that the file encoding is properly handled in your form submission.

Besides that, here are some suggestions on how to debug this further:

  1. Console Logging: Add detailed console.log statements throughout your code:

    • Log the S3_ENDPOINT and BUCKET_NAME you’re using.
    • Log the output of file in the key callback of multerS3. Check if the filename and other properties are as expected.
    • Log the complete error message you receive from DigitalOcean.
  2. Network Inspection: Use your browser’s developer tools (Network tab) to inspect the request being sent to DigitalOcean Spaces. Look at the following:

    • Is the request sent to the correct endpoint?
    • Are the required headers present (e.g., Content-Type, x-amz-... headers for authentication)?
    • Examine the error response from DigitalOcean Spaces for more clues.
  3. Simplify: Temporarily remove some complexity to isolate the problem:

    • Try a direct upload using the aws-sdk without multer-s3 to see if the issue lies within the middleware configuration.
    • Test with a hardcoded filename to eliminate issues with filename generation.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.