Check file size on S3 without downloading?
I have customer files uploaded to Amazon S3, and I would like to add a feature to count the size of those files for each customer. Is there a way to "peek" into the file size without downloading them? I know you can view from the Amazon control panel but I need to do it pro grammatically.
Solution 1:
Send an HTTP HEAD request to the object. A HEAD request will retrieve the same HTTP headers as a GET request, but it will not retrieve the body of the object (saving you bandwidth). You can then parse out the Content-Length header value from the HTTP response headers.
Solution 2:
Node.js example:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
function sizeOf(key, bucket) {
return s3.headObject({ Key: key, Bucket: bucket })
.promise()
.then(res => res.ContentLength);
}
// A test
sizeOf('ahihi.mp4', 'output').then(size => console.log(size));
Doc is here.