admin管理员组

文章数量:1355221

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": "",
            "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.

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 -
.html#createPresignedPost-property

.html

.html

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./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.

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./AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

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

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

Share Improve this question edited Nov 18, 2019 at 2:56 Dev1ce asked Nov 18, 2019 at 2:35 Dev1ceDev1ce 5,95425 gold badges98 silver badges160 bronze badges 6
  • the key in my case should be the AWS S3's folder path yea? – Dev1ce Commented Nov 18, 2019 at 2:55
  • Yeah, sorry. I think I was mistaken. The docs aren't exactly clear on this – Phil Commented Nov 18, 2019 at 2:59
  • 5 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 uses file as the key name for the file. Perhaps try that instead of test.jpg – Phil Commented Nov 18, 2019 at 3:02
  • 4 @Phil it worked! using "file" as the KEY for the file, awesome man, thanks! been banging my head for hours lolx – Dev1ce Commented Nov 18, 2019 at 3: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 Commented Nov 18, 2019 at 3:08
 |  Show 1 more ment

1 Answer 1

Reset to default 8

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.

本文标签: