Ahmed
Ahmed

Reputation: 1359

Why is my s3 createPresignedPost not respecting the content-range-length condition

I am trying to post a file to s3 using createPresignedPost. The file is posting to my bucket but it is not respecting the file size constraint. Here is my code and the file upload is base64 encoded string.

function postObjectSignedUrl(req) {
const key = `${req + "/" + uuid.v4()}`;
return new Promise(function (resolve, reject) {
    const params = {
        Bucket: 'base',
        Expires: 60 * 60, // in seconds,
        Fields: {
            key: key,
        },
        conditions: [
            ['content-length-range', 0,1000000]
        ]


    }
    s3.createPresignedPost(params, (err, data) => {
        if (err) {
            reject(err)
        } else {
            resolve(data);
        }
    })
})

}

My client side code is the following:

var data = new FormData();
const getUrl = await getSignedUrl();
const keys = getUrl["fields"];
$.each(keys, function(key,value){
    data.append(key,value);
});


data.append("file", profilePic);
try {

    const result = await fetch(getUrl["url"], {
        method: "POST",
        mode: "cors",
        headers: {
            'Access-Control-Allow-Origin': '*',
        },
        body: data
    })
    if (result.status === 204){

    }
} catch (err) {
    console.log(err, " error ")
}

Upvotes: 0

Views: 1488

Answers (1)

Hector Martinez
Hector Martinez

Reputation: 427

Normally params attributes in NodeJS SDK are Upper Camel Case so you have to change "conditions" for "Conditions".

BTW you can change your url generator code as follow :)

function postObjectSignedUrl(req) {
    const key = `${req + "/" + uuid.v4()}`;

    const params = {
        Bucket: 'base',
        Expires: 60 * 60, // in seconds,
        Fields: {
            key: key,
        },
        Conditions: [
            ['content-length-range', 0,1000000]
        ]
    }

    return s3.createPresignedPost(params).promise();
})

Regards,

Upvotes: 2

Related Questions