There was a task in which we were required to get each folder name with count of files it contains from an AWS S3 bucket. If we used the general approach to start a counter and increment that in a foreach loop we were getting the exact count of files for each folder but process was taking too long to run. So to reduce the execution time we found another way explained below.
Iterator_count:
We have a folder structure in a bucket like:
//bucket
folder1
folder2
abc.pdf
cdf.pdf
ert.pdf
wcdf.pdf
wert.pdf
folder3
abc.pdf
cdf.pdf
ert.pdf
We want to get each folder names in folder1 as well as a number of files it contains.
We want the result as follows:
folder2=====>>>5
folder3=====>>>3
The code for getting the file count in amazon bucket:
//connect to s3
$credentials = new Credentials(AWSACCESSKEY, AWSSECRETKEY);
$s3Client = S3Client::factory(array( 'credentials' => $credentials ));
//get all the object in s3
$result = $s3Client->ListObjects(array
('Bucket' => 'mybucketname',
'Delimiter' => "/",
'Prefix'=>’folder1/'
)
);
foreach ($result['CommonPrefixes'] as $object)
{
// get count of files in a folder, this is the key method
$count=iterator_count($s3Client->getIterator
('ListObjects', array
(‘'Bucket'=> 'mybucketname',
'Prefix'=> $object['Prefix'],
'Delimiter' => '/'
)
)
);
$msg=$object['Prefix'] .'============== '.$count;
echo "
".$msg;
}
