I am trying to list all the items in my Amazon S3 bucket. I have several nested directories in it.
- dir1/
- dir1/subdir1/
- dir1/subdir2/
- dir1/subdir3/
- dir2/
- dir2/subdir1/
- dir2/subdir2/
- ...
Each subdirectory contains several files. I need to get a nested array with this file structure.
I'm using the Amazon AWS SDK for PHP 2.4.2
This is my code:
$dir = 's3://bucketname';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
echo $file->getType() . ': ' . $file . "\n";
}
However, the result only lists files lying in the bucket, not files lying in directories/subdirectories (files with prefixes) or the directories itself.
If I iterate through ($dir.'/folder')
there is no result at all.
I I pass RecursiveIteratorIterator::SELF_FIRST
as the second argument to the constructor of the iterator, I get only first level directories – no subdirectories.
How can I use the AWS stream wrapper and the PHP RecursiveIterator to list all the files in all the directories in my bucket?
I hope someone can help me.
Thank you!