Skip to content

AWS Upload with multipart/form-data Invalid

I am sending a file to the presignedPOST url to upload to AWS S3 and in other resources I’ve found, to send a file with form-data is to switch to multipart/form-data to send a file.

This is the form data I’ve created is this:

fields['file'] = new File([this.get_compressed_photo],manifest.photo, {type: "image/jpeg"});
var form = new FormData();

for(let field in fields){
    form.append(field+"", fields[field]);
}

try {
    response = await axios.post(my_url, form, {
        headers : {
            "Content-Type" : "multipart/form-data",
        }
    }); 
}catch(error){
    console.log(error);
}

this is the field in the form in the PARAMs for the request:

Content-Disposition: form-data; name="file"; filename="file_name.jpg"
Content-Type: image/jpeg

function() {
    [native code]
}

Is something going wrong here?

UPDATE: AWS does respond, but not with an error that is relevant to the file. I’m not sure if this means that the file is still valid, but just looking at the value for the image file, I’m not sure how.

<Error><Code>SignatureDoesNotMatch</Code>....

I’m using the aws-sdk and creating the presignedPOST url like so:

....
    let path = process.env.PATH + identifier + "/" + file_name;
    var url = false;

    try{
        const url = await s3.createPresignedPost({
            Bucket: process.env.BUCKET,
            Expires: (60 * 5),
            Fields : {
                key: path,
                AWSAccessKeyId: process.env.KEY,
            },
        });
        return url;
    }catch(error){
        return false;
    }
....

Do I still need to add a signature to this?

Answer

I removed the unneeded AWSAccessKeyId in the fields object. In an example somewhere I saw that it was added so I added it initially.

Removing it makes it works like a charm and I think it messes up AWS’s specific required order of the fields.

....
    let path = process.env.PATH + identifier + "/" + file_name;
    var url = false;

    try{
        const url = await s3.createPresignedPost({
            Bucket: process.env.BUCKET,
            Expires: (60 * 5),
            Fields : {
                key: path,          // key is the only required field here
                //AWSAccessKeyId: process.env.KEY, << I COMMENTED OUT THIS LINE
            },
        });
        return url;
    }catch(error){
        return false;
    }
....