4
votes

I'm using the PHP AWS SDK and would like to list all buckets available to me in S3.

I found a few different blocks of documentation that look promising:

Here's what I'm trying, using the AWS Service Builder to initialize an S3 client...

use Aws\Common\Aws;

// Instantiate an S3 client
$aws = Aws::factory(array( 'key' => "MY_KEY", 'secret' => "MY_SECRET"));
$s3 = $aws->get('s3');
$s3->get_bucket_list();

Unfortunately when I run the code I am told that it has no freakin' clue what "get_buckets_list" is. More specifically it says

Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Command was not found matching GetBucketList' in vendor/guzzle/guzzle/src/Guzzle/Service/Client.php:87

So my questions are as follows:

  • Am I looking at the wrong documentation?
  • Is there other documentation somewhere?
  • How do you get a list of buckets using the PHP AWS SDK?
2

2 Answers

9
votes

The documentation for this call can be found here.

$result = $s3->listBuckets(array());
foreach ($result['Buckets'] as $bucket) {
    echo $bucket['Name'], PHP_EOL;
}

I suspect that you were mixing up two different API's :)

2
votes

I found out how to actually do this, but I got here by reverse engineering rather than actually discovering proper documentation. Therefore I consider this to only be a partial answer.

This code will iterate through your buckets and output each name.

use Aws\Common\Aws;

// Instantiate an S3 client
$aws = Aws::factory(array( 'key' => "MY_KEY", 'secret' => "MY_SECRET"));
$s3 = $aws->get('s3');
$s3->get_bucket_list();

$iterator = $s3->getIterator('ListBuckets');
foreach ($iterator as $bucket) {
    echo $bucket['Name'] . "\n";
}