简体   繁体   English

使用PHP SDK列出AWS S3存储桶

[英]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. 我正在使用PHP AWS开发工具包,并希望列出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... 这就是我尝试使用AWS Service Builder初始化S3客户端的方法...

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. 不幸的是,当我运行代码时,我被告知它根本不知道“ get_buckets_list”是什么。 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? 如何使用PHP AWS开发工具包获取存储桶列表?

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 :) 我怀疑您混淆了两种不同的API :)

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM