How to check if DynamoDB table exists?

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

    import boto3
    
    from botocore.exceptions import ClientError
    
    TABLE_NAME = "myTableName"
    dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
    
    table = dynamodb.Table(TABLE_NAME)
    
    try:
        response = client.describe_table(TableName=TABLE_NAME)
    
    except ClientError as ce:
    if ce.response['Error']['Code'] == 'ResourceNotFoundException':
        print "Table " + TABLE_NAME + " does not exist. Create the table first and try again."
    else:
        print "Unknown exception occurred while querying for the " + TABLE_NAME + " table. Printing full error:"
        pprint.pprint(ce.response)
    

提交回复
热议问题