How to check if DynamoDB table exists?

前端 未结 6 2347
情话喂你
情话喂你 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:13

    You can use describe table API to determine whether the table exists.

    Sample code:

    from __future__ import print_function # Python 2/3 compatibility
    import os
    os.environ["TZ"] = "UTC"
    import boto3
    
    client = boto3.client('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")
    
    
    
    response = client.describe_table(
        TableName='Movies'
    )    
    
    print(response)
    

    If table exists:-

    • You will get the response

    If table doesn't exists:-

    • You will get ResourceNotFoundException

      botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotF oundException) when calling the DescribeTable operation: Cannot do operations on a non-existent table

    Another way:-

    Waits until this Table is exists. This method calls DynamoDB.Waiter.table_exists.wait() which polls. DynamoDB.Client.describe_table() every 20 seconds until a successful state is reached. An error is returned after 25 failed checks.

    table.wait_until_exists()
    

提交回复
热议问题