AWS SDK for PHP で S3 アクセス

composer が面倒なので pear で入れちゃいました。

$ sudo pear config-set auto_discover 1
config-set succeeded
$ sudo pear channel-discover pear.amazonwebservices.com
Adding Channel "pear.amazonwebservices.com" succeeded
Discovery of channel "pear.amazonwebservices.com" succeeded
$ sudo pear install aws/sdk
Attempting to discover channel "guzzlephp.org/pear"...
downloading channel.xml ...
Starting to download channel.xml (817 bytes)
....done: 817 bytes
Auto-discovered channel "guzzlephp.org/pear", alias "guzzle", adding to registry
Attempting to discover channel "pear.symfony.com"...
downloading channel.xml ...
Starting to download channel.xml (811 bytes)
...done: 811 bytes
Auto-discovered channel "pear.symfony.com", alias "symfony2", adding to registry
downloading sdk-2.5.1.tgz ...
Starting to download sdk-2.5.1.tgz (1,155,616 bytes)
...done: 1,155,616 bytes
downloading Guzzle-3.8.1.tgz ...
Starting to download Guzzle-3.8.1.tgz (307,810 bytes)
...done: 307,810 bytes
downloading EventDispatcher-2.4.1.tgz ...
Starting to download EventDispatcher-2.4.1.tgz (11,301 bytes)
...done: 11,301 bytes
install ok: channel://pear.symfony.com/EventDispatcher-2.4.1
install ok: channel://guzzlephp.org/pear/Guzzle-3.8.1
install ok: channel://pear.amazonwebservices.com/sdk-2.5.1
$

動作テスト

<?php

require_once('AWSSDKforPHP/aws.phar');

use Aws\S3\S3Client;

$client = S3Client::factory(array(
    'key'    => '<key>',
    'secret' => '<secret>'
));
$buckets = $client->listBuckets();

foreach ($buckets['Buckets'] as $bucket) {
    $name = $bucket['Name'];
    echo "name:$name\n";
    $iterator = $client->getIterator('ListObjects', array(
                                     'Bucket' => $name));
    foreach ($iterator as $object) {
        $key = $object['Key'];
        echo "  key:$key\n";
    }
}

尚、 は「アカウント>セキュリティ認証情報>アクセス証明書>アクセスキー」で分かります。

  • 実行
$ php awstest.php
name:yoya
  key:test/
  key:test/favicon.ico
  key:test/logo64.png

失敗パターン

config を json に分ける方式を試してみたけど、json の中身が謎。とりあえず key と secret を入れたら駄目だった。

{ "key": "<key>", "secret": "<secret>"}
  • さっきとの差分
/*
use Aws\S3\S3Client;
$client = S3Client::factory(array(
    'key'    => 'A',
    'secret' => 'A+O'
));
*/

use Aws\Common\Aws;
$aws = Aws::factory('awsconf.json');
$client = $aws->get('S3');
  • 実行結果
$ php awstest.php
PHP Warning:  Illegal string offset 'params' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24

Warning: Illegal string offset 'params' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24
PHP Notice:  Array to string conversion in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24

Notice: Array to string conversion in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24
PHP Warning:  Illegal string offset 'class' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 52

Warning: Illegal string offset 'class' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 52
PHP Warning:  Illegal string offset 'params' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24

Warning: Illegal string offset 'params' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24
PHP Notice:  Array to string conversion in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24

Notice: Array to string conversion in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 24
PHP Warning:  Illegal string offset 'class' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 52

Warning: Illegal string offset 'class' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilderLoader.php on line 52
PHP Fatal error:  Uncaught exception 'Guzzle\Service\Exception\ServiceNotFoundException' with message 'No service is registered as S3' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php:119
Stack trace:
#0 /home/yoya/awstest.php(17): Guzzle\Service\Builder\ServiceBuilder->get('S3')
#1 {main}
  thrown in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php on line 119

Fatal error: Uncaught exception 'Guzzle\Service\Exception\ServiceNotFoundException' with message 'No service is registered as S3' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php:119
Stack trace:
#0 /home/yoya/test/awstest.php(17): Guzzle\Service\Builder\ServiceBuilder->get('S3')
#1 {main}
  thrown in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php on line 119
$
  • エラーから察するに params がキーなんだなと。
{ "params":{"key": "A", "secret": "A+O"}}
  • 再チャレンジ
$ php awstest.php
PHP Fatal error:  Uncaught exception 'Guzzle\Service\Exception\ServiceNotFoundException' with message 'No service is registered as s3' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php:119
Stack trace:
#0 /home/yoya/awstest.php(17): Guzzle\Service\Builder\ServiceBuilder->get('s3')
#1 {main}
  thrown in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php on line 119

Fatal error: Uncaught exception 'Guzzle\Service\Exception\ServiceNotFoundException' with message 'No service is registered as s3' in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php:119
Stack trace:
#0 /home/yoya/awstest.php(17): Guzzle\Service\Builder\ServiceBuilder->get('s3')
#1 {main}
  thrown in phar:///usr/share/php/AWSSDKforPHP/aws.phar/Guzzle/Service/Builder/ServiceBuilder.php on line 119
$

そもそも s3 が動かない?