Search code examples
phpsdkamazon-s3

List objects in a specific folder on Amazon S3


I am trying to get the list of Object under a specific folder in my bucket.

I know that to get a list of all of my objects I do:

    $objects = $client->getIterator('ListObjects', array(
    'Bucket' => $bucket
)); 

I want to get only the objects under the folder my/folder/test. I have tried adding

        'key' => "my/folder/test",

And

        'prefix' => "my/folder/test",

But it simply returns all of the objects in my bucket.


Solution

  • You need to use Prefix to restrict the search to a specific directory (a common prefix).

    $objects = $client->getIterator('ListObjects', array(
        "Bucket" => $bucket,
        "Prefix" => "your-folder/"
    ));