How to check if DynamoDB table exists?

前端 未结 6 2250
情话喂你
情话喂你 2021-02-01 01:06

I\'m a new user in boto3 and i\'m using DynamoDB.

I went through over the DynamoDB api and I couldn\'t find any method which tell me if a table is already e

6条回答
  •  遇见更好的自我
    2021-02-01 01:22

    You can use .table_status attr of any boto3 Table instance object. It returns it's status if exists (CREATING, UPDATING, DELETING, ACTIVE) or throws exception botocore.exceptions.ClientError: Requested resource not found: Table: not found. You can wrap those conditions into try / except to have full info on the current table state.

    import boto3
    from botocore.exceptions import ClientError
    
    dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
    table = dynamodb.Table('your_table_name_str')
    
    try:
      is_table_existing = table.table_status in ("CREATING", "UPDATING",
                                                 "DELETING", "ACTIVE")
    except ClientError:
      is_table_existing = False
      print "Table %s doesn't exist." % table.name
    

提交回复
热议问题