raphael75
raphael75

Reputation: 3238

How to only list folders in S3 in PHP

I know that to get the list of all the contents of a bucket you do something like this:

$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region'  => 'us-east-1',
'credentials' => array(
    'key' => <key>,
    'secret' => <secret>
)
]);
$objects = $s3->getIterator('ListObjects', array('Bucket' => <bucketname>, 'Prefix' => 'downloads/'));

Is there a way to only get the list of folders inside 1 specific folder instead of the entire recursive list of contents?

Upvotes: 6

Views: 7481

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 927

<?php
    use Aws\S3\S3Client;

    require_once 'vendor/autoload.php';

    $s3 = new Aws\S3\S3Client([
            'version' => 'latest',
            'region'  => 'us-east-1',
            'credentials' => array(
                'key' => <key>,
                'secret' => <secret>
            )
        ]);
    $objects = $s3->ListObjects(['Bucket' => <bucketname>, 'Delimiter'=>'/', 'Prefix' => 'downloads']);
    var_dump($objects);
?>

Upvotes: 3

Related Questions