6

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

Dev1ce
  • 5,390
  • 17
  • 90
  • 150
  • the key in my case should be the AWS S3's folder path yea? – Dev1ce Nov 18 '19 at 02:55
  • Yeah, sorry. I think I was mistaken. The docs aren't exactly clear on this – Phil Nov 18 '19 at 02:59
  • 4
    I have a feeling the problem is your `test.jpg` POST field name for your file object. At a guess, that's being included as part of the preceding fields. [This example](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html) uses `file` as the key name for the file. Perhaps try that instead of `test.jpg` – Phil Nov 18 '19 at 03:02
  • 3
    @Phil it worked! using "file" as the KEY for the file, awesome man, thanks! been banging my head for hours lolx – Dev1ce Nov 18 '19 at 03:05
  • 3
    I don't blame you, that documentation could be a lot better. You should write it up as an answer; I'm sure it will help others in future – Phil Nov 18 '19 at 03:08
  • I disagree that the docs are unclear... see `file` in the form field definitions at https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html. Also, the key word in the error message is **pre** -- S3 is designed not to scan past the first 20K of the form when looking for `file` and throws this error when that happens. The "maximum pre-data length" was exceeded. – Michael - sqlbot Nov 19 '19 at 01:17

1 Answers1

5

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.

tzot
  • 92,761
  • 29
  • 141
  • 204