Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3: MaxPostPreDataLengthExceeded Your POST request fields preceeding the upload file was too large

I am trying to upload a file to AWS S3, using AWS Javascript SDK's createPresignedPost method,
I have the following code to generate the signed credentials for upload -

let AWS = require('aws-sdk');
let util = require('util');

let s3Client = new AWS.S3({
    region: 'us-east-1'
});

let postSignedUrl = async () => {

    try {
        let postSigningParams = {
            Expires: 60,
            Bucket: "some-bucket-name,
            Conditions: [["content-length-range", 100, 10000000]],
            Fields: {
                key: 'test/image.jpg'
            }
        }

        let s3createPresignedPost = util.promisify(s3Client.createPresignedPost).bind(s3Client);
        let postSignedUrl = await s3createPresignedPost(postSigningParams);

        console.log('postSigningParams => ', postSignedUrl);
    } catch (error) {
        console.error(error);
    }
}

postSignedUrl();

I receive credentials like below -

{
            "url": "https://s3.amazonaws.com/some-bucket-name",
            "fields": {
                "key": "test/image.jpg",
                "bucket": "some-bucket-name",
                "X-Amz-Algorithm": "AWS4-HMAC-SHA256",
                "X-Amz-Credential": "some/credentials/us-east-1/s3/aws4_request",
                "X-Amz-Date": "20191118T020945Z",
                "X-Amz-Security-Token": "somesecuritytoken",
                "Policy": "somepolicy",
                "X-Amz-Signature": "somesignature"
            }
}

But when I try to upload an image using the above creds using POSTMAN tool,
I am not able to do so.

enter image description here

I double-checked my file size, and it's 5 MB,
while the range that I've set while creating the signed url is between 100 to 10000000 bytes

References -
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

https://blog.bigbinary.com/2018/09/04/uploading-files-directly-to-s3-using-pre-signed-post-request.html

https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

like image 281
Ani Avatar asked Nov 18 '19 02:11

Ani


1 Answers

You did not include a form field named file with the contents of your test.jpg in your form data.

A typical way to do what you want using curl is:

curl -X POST -F Content-Type=$content_type -F key=$key \
     -F acl=$acl -F Policy=$policy -F X-Amz-Credential=$credential \
     -F X-Amz-Algorithm=$algorithm -F X-Amz-Storage-Class=$storage_class \
     -F file=@$your_file_name $form_action

In the example code above, I specified all variables (what literally varies between POSTs :) ) using shell notation (ie prefixed with $). Note that whenever curl sees a field value prefixed with @ it uses the rest of the field value as a filename and uses the contents of the filename as field value.

like image 100
tzot Avatar answered Sep 28 '22 10:09

tzot