检查 DynamoDB 中是否存在表的最佳方法是什么?

检查 DynamoDb 中是否存在表的最佳方法是什么?

What is the best way to check if table exists in DynamoDb?

如果代码在 PHP 中,我将不胜感激.

I would appreciate it if the code would be in PHP.

无论是否活跃.

* 稍后作为示例添加到错误代码 400 的各种情况

* Added later as an example to various cases for error code 400

检查表是否存在很容易,它可以有以下之一TableStatus => 正在创建、活动、删除或更新

It's very easy to check if the table exist, it can have one of the following TableStatus => CREATING, ACTIVE, DELETING or UPDATING

但如果我收到错误 400,它可能意味着不止一件事.

but in case i get error 400 it can mean more than one thing.

1) 错误地将空字符串作为表名发送.

1) sent null string as a table name by mistake.

[x-aws-body] => {"TableName":""})

[x-aws-body] => {"TableName":""} )

[body] => CFSimpleXML Object
    (
        [__type] => com.amazon.coral.validate#ValidationException
        [message] => The paramater 'tableName' must be at least 3 characters long and at most 255 characters long
    )

[status] => 400

2) 发送到 DynamoDB 的命令中存在语法错误,例如写入 table_name 而不是 table_name.

2) syntax error in the command sent to DynamoDB, for example writting tabel_name instead of table_name.

[x-aws-body] => {"TabelName":"test7"})

[x-aws-body] => {"TabelName":"test7"} )

[body] => CFSimpleXML Object
    (
        [__type] => com.amazon.coral.validate#ValidationException
        [message] => The paramater 'tableName' is required but was not present in the request
    )

[status] => 400

3) 我会猜测但没有检查,如果我同时超过了桌子上的预置容量.

3) I would guess but didn't check, if I exceed at that same time the provisioned capacity on the table.

推荐答案

你可以看看官方PHP SDK的describe_table".400 表示不存在" 官方文档中有一个相当广泛的示例.看看它在删除"示例中是如何使用的,就在底部.

You can have a look at "describe_table" of the official PHP SDK. 400 means "does not exist" There is a pretty extensive example in the official documentation. Look at how it is used in the "delete" example, right at the bottom.

http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html

这是文档中的(剥离的)示例

Here is the (stripped) example from the doc

<?php
require_once dirname(__FILE__) . '/sdk/sdk.class.php';

$dynamodb = new AmazonDynamoDB();
$table_name = 'ExampleTable';
$response = $dynamodb->describe_table(array('TableName' => $table_name));

if((integer) $response->status !== 400)
{
    $error_type = $response->body->__type;
    $error_code = explode('#', $error_type)[1];
    if($error_code == 'ResourceNotFoundException')
    {
        echo "Table ".$table_name." exists.";
    }
}
?>

相关文章