DynamoDb client

This page contains examples with the DynamoDb client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.

The DynamoDb package could be installed with Composer.

composer require async-aws/dynamo-db

A new client object may be instantiated by:

use AsyncAws\DynamoDb\DynamoDbClient; $dynamoDb = new DynamoDbClient();

The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:

use AsyncAws\DynamoDb\DynamoDbClient; $dynamoDb = new DynamoDbClient([ 'accessKeyId' => 'my_access_key', 'accessKeySecret' => 'my_access_secret', 'region' => 'eu-central-1', ]);

For all available options, see the configuration reference.

Usage

Create a table

use AsyncAws\DynamoDb\DynamoDbClient; use AsyncAws\DynamoDb\Input\CreateTableInput; use AsyncAws\DynamoDb\Input\DescribeTableInput; use AsyncAws\DynamoDb\ValueObject\AttributeDefinition; use AsyncAws\DynamoDb\ValueObject\KeySchemaElement; use AsyncAws\DynamoDb\ValueObject\ProvisionedThroughput; $dynamoDb = new DynamoDbClient(); $dynamoDb->createTable(new CreateTableInput([ 'TableName' => 'errors', 'AttributeDefinitions' => [ new AttributeDefinition(['AttributeName' => 'id', 'AttributeType' => 'N']), new AttributeDefinition(['AttributeName' => 'time', 'AttributeType' => 'N']), ], 'KeySchema' => [ new KeySchemaElement(['AttributeName' => 'id', 'KeyType' => 'HASH']), new KeySchemaElement(['AttributeName' => 'time', 'KeyType' => 'RANGE']), ], 'ProvisionedThroughput' => new ProvisionedThroughput([ 'ReadCapacityUnits' => 10, 'WriteCapacityUnits' => 20 ]), ])); // Wait for end of creation $dynamoDb->tableExists(new DescribeTableInput(['TableName' => 'errors']))->wait();

Update a table

use AsyncAws\DynamoDb\DynamoDbClient; use AsyncAws\DynamoDb\Input\UpdateTableInput; use AsyncAws\DynamoDb\ValueObject\ProvisionedThroughput; $dynamoDb = new DynamoDbClient(); $dynamoDb->updateTable(new UpdateTableInput([ 'TableName' => 'errors', 'ProvisionedThroughput' => new ProvisionedThroughput([ 'ReadCapacityUnits' => 15, 'WriteCapacityUnits' => 25 ]), ]));

Insert an item.

use AsyncAws\DynamoDb\DynamoDbClient; use AsyncAws\DynamoDb\Input\PutItemInput; use AsyncAws\DynamoDb\ValueObject\AttributeValue; $dynamoDb = new DynamoDbClient(); $result = $dynamoDb->putItem(new PutItemInput([ 'TableName' => 'errors', 'Item' => [ 'id' => new AttributeValue(['N' => '1337']), 'time' => new AttributeValue(['N' => (string) time()]), 'error' => new AttributeValue(['S' => 'Executive overflow']), 'message' => new AttributeValue(['S' => 'no vacant areas']), ], ])); echo 'Consumed capacity: ' . ($result->getConsumedCapacity() ? $result->getConsumedCapacity()->getCapacityUnits() : null);

Get table information

use AsyncAws\DynamoDb\DynamoDbClient; use AsyncAws\DynamoDb\Input\DescribeTableInput; $dynamoDb = new DynamoDbClient(); $result = $dynamoDb->describeTable(new DescribeTableInput([ 'TableName' => 'errors', ])); echo 'Item count: ' . $result->getTable()->getItemCount(); echo 'Read capacity: ' . $result->getTable()->getProvisionedThroughput()->getReadCapacityUnits() . "\n";

Get an item.

use AsyncAws\DynamoDb\DynamoDbClient; use AsyncAws\DynamoDb\Input\GetItemInput; use AsyncAws\DynamoDb\ValueObject\AttributeValue; $dynamoDb = new DynamoDbClient(); $result = $dynamoDb->getItem(new GetItemInput([ 'TableName' => 'errors', 'ConsistentRead' => true, 'Key' => [ 'id' => new AttributeValue(['N' => '1201']), 'time' => new AttributeValue(['N' => '1585503599']), ], ])); echo 'Id: '.$result->getItem()['id']->getN() . PHP_EOL; echo 'Error: '.$result->getItem()['error']->getS() . PHP_EOL; echo 'Message: '.$result->getItem()['message']->getS() . PHP_EOL;

The source code to this page is found on GitHub.