Retrieve list of only root objects (folders) in S3

Given my S3 bucket that contains images in a structure like so:

root/portraits/portrait_001.jpg
root/landscapes/landscape_001.jpg

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?

portraits/
landscapes/

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.

See: Listing Keys Hierarchically Using a Prefix and Delimiter

链接地址: http://www.djcxy.com/p/87598.html

上一篇: 单击时选择HTML文本输入中的所有文本

下一篇: 在S3中仅检索根对象(文件夹)的列表