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

后端 未结 6 2101
粉色の甜心
粉色の甜心 2021-02-19 22:28

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

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

Either active or not.

* Added late

6条回答
  •  被撕碎了的回忆
    2021-02-19 22:48

    I think the answer that solves this with describeTable is a good one, but fooling around with the status code response makes the code less readable and more confusing.

    I chose to check for a tables existence using listTables. Here are the docs

    $tableName = 'my_table';
    
    $client = DynamoDbClient::factory(array('region' => 'us-west-2'));
    
    $response = $client->listTables();
    
    if (!in_array($tableName, $response['TableNames'])) {
        // handle non-existence.
        // throw an error if you want or whatever
    }
    
    // handle existence
    echo "Table " . $tableName . " exists";
    

提交回复
热议问题