使用 boto3 在 python 中使用另一个现有表的结构创建 DynamoDB 表时出错



我正在使用下面的代码来创建新的 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:UsersADMINAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesbotocoreclient.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:UsersADMINAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesbotocoreclient.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)

最新更新