I have finally figured how to get the contents of a folder (files AND "subfolders") without having to load up the entire contents of the bucket each time.
First an example array to pass in:
$options = array(
"Bucket" => $bucket,
"Prefix" => "rootFolder/",
"Delimiter" => "/"
);
This creates an Iterator that will only show you the "files":
$objects = $s3Client->getIterator('ListObjectsV2', $options);
so you can only do this (which does not include "folders" because they end with "/"):
foreach ($objects as $object) {
echo '<br>' . $object['Key'] . ' ' . $object['Size'];
} //for
Instead, do this to get the whole object passed back:
$contents = $s3Client->listObjectsV2($options);
Then you get the whole object which contains an array called "Common Prefixes" that looks like this and gives you the "folders":
[CommonPrefixes] => Array (
[0] => Array ([Prefix] => Folder 1/)
[1] => Array ([Prefix] => Carly/)
)
And you get another array called Contents which is what the getIterator call returns (your files):
[Contents] => Array
(
[0] => Array
(
[Key] => rootFolder/myFile.txt
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2021-11-18 08:56:53.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "d41d8cd98f00b204e9800998ecf8427e"
[Size] => 0
[StorageClass] => STANDARD
)
[1] => Array
...
I have finally figured how to get the contents of a folder (files AND "subfolders") without having to load up the entire contents of the bucket each time.
First an example array to pass in:
$options = array(
"Bucket" => $bucket,
"Prefix" => "rootFolder/",
"Delimiter" => "/"
);
This creates an Iterator that will only show you the "files":
$objects = $s3Client->getIterator('ListObjectsV2', $options);
so you can only do this (which does not include "folders" because they end with "/"):
foreach ($objects as $object) {
echo '<br>' . $object['Key'] . ' ' . $object['Size'];
} //for
Instead, do this to get the whole object passed back:
$contents = $s3Client->listObjectsV2($options);
Then you get the whole object which contains an array called "Common Prefixes" that looks like this and gives you the "folders":
[CommonPrefixes] => Array (
[0] => Array ([Prefix] => Folder 1/)
[1] => Array ([Prefix] => Carly/)
)
And you get another array called Contents which is what the getIterator call returns (your files):
[Contents] => Array
(
[0] => Array
(
[Key] => rootFolder/myFile.txt
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2021-11-18 08:56:53.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "d41d8cd98f00b204e9800998ecf8427e"
[Size] => 0
[StorageClass] => STANDARD
)
[1] => Array
...