Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing AWS S3 buckets using the PHP SDK

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:

  • http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/get_bucket_list
  • http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.Iterator.ListBucketsIterator.html

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?
like image 933
slifty Avatar asked Dec 20 '22 20:12

slifty


1 Answers

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 :)

like image 109
Ja͢ck Avatar answered Dec 22 '22 10:12

Ja͢ck