forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTables_CRUD.php
More file actions
121 lines (91 loc) · 3.02 KB
/
Tables_CRUD.php
File metadata and controls
121 lines (91 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[dynamodb.php.codeexample.Tables_CRUD]
require 'vendor/autoload.php';
date_default_timezone_set('UTC');
use Aws\DynamoDb\Exception\DynamoDbException;
$sdk = new Aws\Sdk([
'region' => 'us-west-2',
'version' => 'latest'
]);
$dynamodb = $sdk->createDynamoDb();
$tableName = 'ExampleTable';
echo "# Creating table $tableName...\n";
try {
$response = $dynamodb->createTable([
'TableName' => $tableName,
'AttributeDefinitions' => [
[
'AttributeName' => 'Id',
'AttributeType' => 'N'
]
],
'KeySchema' => [
[
'AttributeName' => 'Id',
'KeyType' => 'HASH' //Partition key.
]
],
'BillingMode' => 'PAY_PER_REQUEST' // Use on-demand billing mode.
]);
$dynamodb->waitUntil('TableExists', [
'TableName' => $tableName,
'@waiter' => [
'delay' => 5,
'maxAttempts' => 20
]
]);
print_r($response->getPath('TableDescription'));
echo "table $tableName has been created.\n";
} catch (DynamoDbException $e) {
echo $e->getMessage() . "\n";
exit("Unable to create table $tableName\n");
}
####################################################################
# Updating the table
// No need to update provisioned throughput as we are using PAY_PER_REQUEST mode.
echo "# Skipping table update for throughput settings (not needed for PAY_PER_REQUEST).\n";
####################################################################
# Deleting the table
try {
echo "# Deleting table $tableName...\n";
$response = $dynamodb->deleteTable([ 'TableName' => $tableName]);
$dynamodb->waitUntil('TableNotExists', [
'TableName' => $tableName,
'@waiter' => [
'delay' => 5,
'maxAttempts' => 20
]
]);
echo "The table has been deleted.\n";
} catch (DynamoDbException $e) {
echo $e->getMessage() . "\n";
exit("Unable to delete table $tableName\n");
}
####################################################################
# List all table names for this AWS account, in this region
echo "# Listing all of your tables in the current region...\n";
$tables = [];
// Walk through table names, two at a time
unset($response);
do {
if (isset($response)) {
$params = [
'Limit' => 2,
'ExclusiveStartTableName' => $response['LastEvaluatedTableName']
];
} else {
$params = ['Limit' => 2];
}
$response = $dynamodb->listTables($params);
foreach ($response['TableNames'] as $key => $value) {
echo "$value\n";
}
$tables = array_merge($tables, $response['TableNames']);
} while ($response['LastEvaluatedTableName']);
// Print total number of tables
echo "Total number of tables: ";
print_r(count($tables));
echo "\n";
// snippet-end:[dynamodb.php.codeexample.Tables_CRUD]