Given my S3 bucket that contains images in a structure like so:
where root is my bucket, and there are no other files in my root, just those folders (objects), how do I retrieve a list of just those objects?
I am familiar with using the Delimiter and Prefix in the ListObjects call.
If I do the following, I get no results:
$objects = $s3->getIterator('ListObjects', array( 'Bucket' => $bucket, 'Delimiter' => '/', )); foreach($objects as $object) echo $object['Key'] . "\n";
If I don’t use a Delimiter, I get everything, obviously.
I cannot use a prefix because the objects I desire are root-level. Otherwise, I have no problem using the prefix to say, list just the files in ‘portraits/’
From my searches, I’ve only managed to find solutions from previous years that only apply to the aws php sdk v1 or v2, and I have had no luck in trying those (v3 is quite different)
Any suggestions? I feel like I’m missing something simple, but searching through the documentation, I can’t find anything to help me. As a last resort, I’ll just have to stick with manually declaring an array
$categories = ['portraits/', 'landscapes/']
But that isn’t ideal in the case where I want to add more categories in the future, and not have to worry about adding another category manually.
Any help would be greatly appreciated 🙂
Edit – Solution
I must have been looking in the wrong places during my object dumps, but eventually saw the Common Prefixes in the returned result from a ListObjects call with a delimiter of ‘/’, like so:
$s3->listObjects(array('Bucket' => $bucket, 'Delimiter' => '/'));
Directories do not actually exist in Amazon S3. However, the Management Console allows the creation of folders, and paths are supported to give the illusion of directories.
For example, object
bar.jpg stored in the
foo directory has a path of
/foo/bar.jpg. The trick is that the object is actually called
foo/bar.jpg rather than just
bar.jpg. Most users wouldn’t even notice the difference.
From the API, the ability to list directories is provided via the concept of
CommonPrefixes, which look the same as directory paths and consist of the portion of object names (‘keys’) before the final slash.