簡體   English   中英

使用 boto3 在 python 中使用另一個現有表的結構創建 DynamoDB 表時出錯

[英]Error while creating DynamoDB table using another existing table's structure in python using boto3

我正在使用下面的代碼來創建新的 DynamoDB 表。

import boto3
import os

dynamoclient = boto3.client('dynamodb', region_name='eu-west-1',
    aws_access_key_id='ACCESS_KEY_SOURCE',
    aws_secret_access_key='SECRET_KEY_SOURCE')
dynamoresource = boto3.resource('dynamodb', region_name='eu-west-1',
    aws_access_key_id='ACCESS_KEY_SOURCE',
    aws_secret_access_key='SECRET_KEY_SOURCE')

dynamotargetclient = boto3.client('dynamodb', region_name='us-west-1',
    aws_access_key_id='ACCESS_KEY_TARGET',
    aws_secret_access_key='SECRET_KEY_TARGET')

curr_table=dynamoresource.Table('geo-test')


table = dynamotargetclient.create_table(
        TableName='geo-test-1',
        KeySchema=curr_table.key_schema,
        AttributeDefinitions=curr_table.attribute_definitions,
        
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5
        })

所以基本上,我正在創建一個表並從我的 AWS 賬戶復制一些其他 AWS 賬戶中的項目。 但是,我在創建一個時遇到了錯誤。

錯誤如下。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\botocore\client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\botocore\client.py", line 635, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions

下表“地理測試”的描述如下。

{'Table': {'AttributeDefinitions': [{'AttributeName': 'geohash', 'AttributeType': 'N'}, {'AttributeName': 'hashKey', 'AttributeType': 'N'}, {'AttributeName': 'rangeKey', 'AttributeType': 'S'}], 'TableName': 'geo-test', 'KeySchema': [{'AttributeName': 'hashKey', 'KeyType': 'HASH'}, {'AttributeName': 'rangeKey', 'KeyType': 'RANGE'}], 'TableStatus': 'ACTIVE', 'CreationDateTime': datetime.datetime(2020, 6, 7, 11, 16, 23, 460000, tzinfo=tzlocal()), 'ProvisionedThroughput': {'NumberOfDecreasesToday': 0, 'ReadCapacityUnits': 10, 'WriteCapacityUnits': 5}, 'TableSizeBytes': 1104, 'ItemCount': 6, 'TableArn': 'arn:aws:dynamodb:us-east-1:498526653762:table/geo-test', 'TableId': '4946225b-6342-41b9-bac0-9b27e9e17cad', 'LocalSecondaryIndexes': [{'IndexName': 'geohash-index', 'KeySchema': [{'AttributeName': 'hashKey', 'KeyType': 'HASH'}, {'AttributeName': 'geohash', 'KeyType': 'RANGE'}], 'Projection': {'ProjectionType': 'ALL'}, 'IndexSizeBytes': 1104, 'ItemCount': 6, 'IndexArn': 'arn:aws:dynamodb:us-east-1:498526653762:table/geo-test/index/geohash-index'}]}, 'ResponseMetadata': {'RequestId': 'V93HT96RDPP2NETR5RAAV2HQO3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 23 Jun 2020 05:22:07 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '943', 'connection': 'keep-alive', 'x-amzn-requestid': 'V93HT96RDPP2NETR5RAAV2HQO3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '3098259971'}, 'RetryAttempts': 0}}

相同的代碼適用於另一個表,其中沒有 LSI。 任何幫助,將不勝感激。 謝謝 !!!

我創建了一個github 存儲庫,其中包含我前段時間用來將 dynamodb 表從一個帳戶復制到另一個帳戶的腳本。 代碼是用葡萄牙語編寫的,所以我不知道它是否有幫助。 但是要解決這個特殊問題,您可以做的是:

table = dynamodb_client.describe_table(TableName="table_name")

attrs = []
keys = []

for k in table['Table']['KeySchema']:
    keys.append(k['AttributeName'])

for attr in table['Table']['AttributeDefinitions']:
    if attr['AttributeName'] in keys:
        attrs.append(attr)


dynamodb_client.create_table(
    KeySchema=table['Table']['KeySchema'],
    AttributeDefinitions=attrs,
    other-options-here)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM