Home > OS >  AWS S3 POST Policy content-length-range doesn't work for exact file size match
AWS S3 POST Policy content-length-range doesn't work for exact file size match

Time:01-20

I'd like to add file size validation to our signed urls, making sure the client uploads a file of exactly the size I signed it with. However, when I do this :

"conditions": [
    {"acl": "public-read"},
    .... ,
    ["content-length-range", 1024, 1024]
]

For small files, it works.
But a bit larger files, for example 25mb, it results in EntityTooSmall error.

It starts working only if I set the minimum to 0 like this :

["content-length-range", 0, 1024]

But I want to enforce a specific file size, and not a range.
Does S3 not support exact file size match?

CodePudding user response:

Unfortunately, no. You can specify only a range. I would check if the file size limit is specified correctly. This error likely indicates that the calculation wasn't right.

To test it further, I'd suggest generating a file with the exact size using e.g. dd tool, like this

dd if=/dev/zero of=test-file-1 bs=26214400 count=1

Then try to upload it. In this case, your range must be [26214400,26214400].

CodePudding user response:

I managed to test with Postman an exact file size and everything is working correctly. The condition that I used is:

const Conditions = [
    ["content-length-range", 30789588, 30789588],
    ["eq", "$Content-Type", "image/jpeg"]
];

I downloaded the 30MB file from here: https://sample-videos.com/download-sample-jpg-image.php

When I change the size of the content length to 30789587 I get the following:

<Error>
    <Code>EntityTooLarge</Code>
    <Message>Your proposed upload exceeds the maximum allowed size</Message>
    <ProposedSize>30789588</ProposedSize>
    <MaxSizeAllowed>30789587</MaxSizeAllowed>
</Error> 

And with a content length of 30789589:

<Error>
    <Code>EntityTooSmall</Code>
    <Message>Your proposed upload is smaller than the minimum allowed size</Message>
    <ProposedSize>30789588</ProposedSize>
    <MinSizeAllowed>30789589</MinSizeAllowed>
</Error> 

Test with the same file I used and let's see if it works for you.

  • Related