Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve list of only root objects (folders) in S3 - aws sdk v3 php

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' => '/'));
like image 735
Anthony S Avatar asked Aug 21 '15 04:08

Anthony S


People also ask

How can I get only one level of objects in a S3 bucket?

How can I get only one level of objects in a S3 bucket? To list only the root level objects in the bucket, you send a GET request on the bucket with the slash ( / ) delimiter character. In response, Amazon S3 returns the sample.

How do I extract files from S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it.

What is list_objects_v2?

PDF. Returns some or all (up to 1,000) of the objects in a bucket with each request. You can use the request parameters as selection criteria to return a subset of the objects in a bucket. A 200 OK response can contain valid or invalid XML.

How do you show objects and folders in a bucket?

In the left navigation pane, choose Buckets. In the Buckets list, choose the name of the bucket in which your folder is stored. In the Objects list, select the check box next to the name of the folder. Choose Actions, and then choose Calculate total size.


1 Answers

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

like image 165
John Rotenstein Avatar answered Oct 18 '22 09:10

John Rotenstein