1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for PHP v3 Preview with JSON support

Posted at

AWS SDK for PHP v3 で非同期を試してみたのと、ブランチで開発された json サポートをパッチとして適用してみて動作確認してみた。

composer.json

{
    "require": {
        "aws/aws-sdk-php": "~3.0@dev"
    }
}

jsonサポートをパッチとして適用してみる

$ cd vendor/aws/aws-sdk-php
$ wget https://github.com/aws/aws-sdk-php/commit/fb0d0b98918477af3b46165036928493c6b8a72b.diff
$ patch -p1 < fb0d0b98918477af3b46165036928493c6b8a72b.diff
patching file src/DynamoDb/Marshaler.php
patching file tests/DynamoDb/DynamoDbClientTest.php
patching file tests/DynamoDb/MarshalerTest.php

DynamoDBにデータを入れてみる

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;

$ddbClient = DynamoDbClient::factory([
    'region'  => 'ap-northeast-1',
    'version' => 'latest',
    'key'     =>  '<your aws key>',
    'secret'  =>  '<your aws secret>'
]);

$doc = <<<JSON
{
  "content": {
    "to": ["123412"],
	"id": "4444",
	"text": "ほげほげ",
	"createdTime": 1418790948656,
  },
  "createdTime": 1418790948679,
  "id": "1234",
  "to": ["12341234"]
}
JSON;

/** @var \Aws\Common\FutureResult $ddbResult */
$ddbResult = $ddbClient->putItem([
    'TableName' => 'table_name',
    'Item' => [
        'id'          => [ 'S' => $id ],
        'createdTime' => [ 'N' => (string)$createdTime ],
        'doc'         => [ 'M' => (new Marshaler)->marshalDocument(json_encode($doc)) ],
    ],
    '@future' => true
]);

$dbbResult->then(
    function ($result) {
        /** @var Aws\Common\Result $result **/
        echo 'done';
    },
    function (\Exception $e) {
        echo 'fail';
    }
);

createdTime のところを文字列にキャストしているのは、Error executing Aws\DynamoDb\DynamoDbClient::putItem() on "https://dynamodb.ap-northeast-1.amazonaws.com"; SerializationException (client error): class java.lang.Long can not be converted to an String って起こられたので...。

こんな感じにデータが入りましたとさ

スクリーンショット 2014-12-17 14.32.58.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?