Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Get Size of Files in Amazon S3 Bucket

I've seen previous questions on "How to find the size of an entire S3 bucket." This is somewhat of a different question-- so I apologize in advance if it is vague. I'll do my best to explain what I am trying to achieve.

I am currently using Amazon S3 PHP Class.

This is what I am trying to achieve:

  1. I would like to be able to loop through my MySQL database and get specific filenames of files on my S3 server. (This may be based on user).

  2. During the loop, query the Amazon S3 bucket (somehow) and get the filesize of each file that was in my MySQL loop.

  3. Add up all the filesizes, to get one total approximate bytesize.

So, essentially, let's say I have a user that has 5 files on the S3 bucket. How might I be able to query the S3 bucket, to see how much data this user's 5 files is storing in my bucket?

My apologies if this is difficult to understand. I can re-articulate, if needed.

Any nudge in the right direction would be greatly appreciated.

like image 462
Dodinas Avatar asked Dec 15 '10 17:12

Dodinas


People also ask

How can I get the size of an Amazon S3 bucket?

To find the size of a single S3 bucket, you can use the S3 console and select the bucket you wish to view. Under Metrics, there's a graph that shows the total number of bytes stored over time.

What is the size of object in S3?

Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 TB. The largest object that can be uploaded in a single PUT is 5 GB.

How do I see how many files are in a S3 bucket?

Go to AWS Billing, then reports, then AWS Usage reports. Select Amazon Simple Storage Service, then Operation StandardStorage. Then you can download a CSV file that includes a UsageType of StorageObjectCount that lists the item count for each bucket.


1 Answers

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'];
like image 72
John Ventimiglia Avatar answered Sep 16 '22 18:09

John Ventimiglia