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

后端 未结 6 2106
粉色の甜心
粉色の甜心 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:53

    Some of these answers are using the older SDK's and so I thought I'd update this useful question with what I coded up and works well. The newer exceptions really do make this task easier. This function gives you a nice boolean to use in scripts.

    use Aws\DynamoDb\Exception\ResourceNotFoundException; // <-- make sure this line is at the top
    
        public function TableExists($tableName) {
    
        $ddb = DynamoDbClient::factory(array('region' => 'us-east-1')); // EC2 role security
    
        try {
            $result = $ddb->describeTable(array(
                "TableName" => $tableName
            ));
        } catch (ResourceNotFoundException $e) {
            // if this exception is thrown, the table doesn't exist
            return false;
        }
    
        // no exception thrown? table exists!
        return true;
    }
    

    Hopefully this complete working code helps some of you.

提交回复
热议问题