I have a long and still growing policy within one of my S3 buckets. I want to check the total length to see, if the policy may hit the 20KB hard limit of AWS anytime soon.
I did following in my js console:
policy = <text copied from s3 bucket policy>
JSON.stringify(policy).length
> 28941
The policy works even though I should be over the limit. How can this be? How can I reliably check the policy size in an AWS approved manner and see, how many characters are still left?
I did following:
- checked the AWS docs
- counted the characters myself
2
Answers
I am not aware of any S3 API that can simply return the size of a bucket policy.
You can compute the size of a bucket policy as a JSON object, just like any other JSON object. It looks like you are using JavaScript. In JavaScript, the
length
property of a string contains the length of the string in UTF-16 code units, not the size of the string in bytes.Getting the size of a JSON object depends on the JS runtime you are using. For example, if you are using node.js or a modern browser, you can convert your JSON to an
Uint8Array
and infer the size from the length of that array (inUint8Array
each item is a 1 byte unsigned integer).After contacting AWS support on this it turned out that:
JSON.stringify(policy)
may report.JSON.stringify(policy).length < 20kB
then you are safe, else consider a workaround soon.