The getIterator()
method has a third parameter for options specific to the iterator object. The S3 ListObjects iterator is actually a concrete class and specifies some custom options in its docs: return_prefixes
, sort_results
, and names_only
(see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.Iterator.ListObjectsIterator.html). Use return_prefixes
to get the CommonPrefixes
intermingled with the objects. Try this:
$iterator = $client->getIterator(
'ListObjects',
array(
'Bucket' => $bucket,
'Prefix' => $dir,
'Delimiter' => '/'
),
array(
'return_prefixes' => true,
),
);
foreach ($iterator as $object) {
if (isset($object['Prefix'])) {
// For Common Prefixes
echo $object['Prefix'] . "<br/>\n";
} else {
// For Objects
echo $object['Key'] . "<br/>\n";
}
}
// Also Licensed under version 2.0 of the Apache License.