Dynamically Get Size of Files in Amazon S3 Bucket

You really should be storing the filesize in your Mysql table. Faster to get the info, less S3 cost.

But... try this?

$s3 = new Aws\S3\S3Client($parameters);
$obj_data = $s3->headObject([
   'Bucket' => $bucket,
   'Key'    => $key
]);
echo 'Size is: '.$obj_data['ContentLength'];

Now that AWS has released a support PHP SDK, you might want to consider looking into switching to use that: http://aws.amazon.com/sdkforphp/.

It also includes api for getting an object size: get_object_filesize().

I agree with John Ventimiglia. Each call to S3 take time and costs money. If you can store the information locally, it will provide a better customer experience (faster) and be cheaper.


In 2015 I have to use http://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-s3-stream-wrapper.html for that

$client->registerStreamWrapper();

and then

$size = filesize('s3://' . $bucket . '/' . $key);

I hope that will helps to someone.