Edited at

AWS:DynamoDBにphpで接続するメモ

More than 3 years have passed since last update.

1.テーブル作成

AWSログイン後、

サービス=>DynamoDB=>テーブル作成

テーブル名:test1

プライマリキー:id1 (文字列)

=>テーブル作成

しばらくまって管理画面から"test1"=>項目=>項目の作成

+をクリックして"append"で"val1"を追加

id1,val1に値を入れて保存

■EC2(Linux)にて

2.まずAWS SDK for PHPを入れる。

$ curl -sS https://getcomposer.org/installer

$ php composer.phar require aws/aws-sdk-php
./composer.json is not writable.
以前sudoで入れたのがよくなかったのでownerをec2-userにもどす。通常は不要
$ sudo chown ec2-user.ec2-user composer.*
$ sudo chown -R ec2-user.ec2-user vendor

再度実行。しばらくまって下のような出力なら正常終了。

Using version ^2.8 for aws/aws-sdk-php

./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing symfony/event-dispatcher (v2.8.6)
Downloading: 100%

- Installing guzzle/guzzle (v3.9.3)
Downloading: 100%

- Installing aws/aws-sdk-php (2.8.30)
Downloading: 100%

symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
guzzle/guzzle suggests installing guzzlehttp/guzzle (Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated.)
aws/aws-sdk-php suggests installing doctrine/cache (Adds support for caching of credentials and responses)
aws/aws-sdk-php suggests installing ext-apc (Allows service description opcode caching, request and response caching, and credentials caching)
aws/aws-sdk-php suggests installing monolog/monolog (Adds support for logging HTTP requests and responses)
aws/aws-sdk-php suggests installing symfony/yaml (Eases the ability to write manifests for creating jobs in AWS Import/Export)
Writing lock file
Generating autoload files

~~~~~~~~~~~~~~~~~~~~~~~~

3.以下の2ファイルを作る。


dynamo.php

<?php

require '/home/ec2-user/vendor/autoload.php';

use Aws\Common\Aws;
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;

$aws = Aws::factory("aws_config.php");
$client = $aws->get('dynamodb');
$get_item_array['ConsistentRead'] = true;
$get_item_array['TableName'] = 'test1';
$get_item_array['Key']['id1'] = array('S'=>'a');
$result = $client->getItem($get_item_array);
var_dump($result);



aws_config.php

<?php

return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => <アクセスキー ID>,
'secret' => <シークレットアクセスキー>,
'region' => 'ap-northeast-1'
)
)
)
);

4.実行する

$ php -f dynamo.php

(こんな表示が出れば成功)
object(Guzzle\Service\Resource\Model)#75 (2) {
["structure":protected]=>
NULL
["data":protected]=>
array(1) {
["Item"]=>
array(2) {
["val1"]=>
array(1) {
["S"]=>
string(1) "b"
}
["id1"]=>
array(1) {
["S"]=>
string(1) "a"
}
}
}
}


<テーブル名を間違っているときのエラー>

PHP Fatal error: Uncaught Aws\DynamoDb\Exception\ResourceNotFoundException: AWS Error Code: ResourceNotFoundException, Status Code: 400, AWS Request ID: MBKMS0J37VFMPE2FTJSABOPJFJVV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Type: client, AWS Error Message: Requested resource not found, User-Agent: aws-sdk-php2/2.8.30 Guzzle/3.9.3 curl/7.40.0 PHP/5.3.29

thrown in /home/ec2-user/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

<項目名を間違っているときのエラー>

PHP Fatal error: Uncaught Aws\DynamoDb\Exception\ValidationException: AWS Error Code: ValidationException, Status Code: 400, AWS Request ID: CK4A4NDJ5BAAS5JNR9F10OHQ8VVV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Type: client, AWS Error Message: The provided key element does not match the schema, User-Agent: aws-sdk-php2/2.8.30 Guzzle/3.9.3 curl/7.40.0 PHP/5.3.29

thrown in /home/ec2-user/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91