AWS DynamoDB - 使用JSON文件作为输入,使用Boto3加载数据

use*_*107 5 python boto amazon-web-services amazon-dynamodb boto3

我在Amazon DynamoDB中有多个表,JSON数据当前使用batch-write-item作为AWS CLI一部分提供的命令上传到表中- 这很有效.

但是我想只使用Python + Boto3但是无法使用外部数据文件作为输入执行Boto BatchWriteItem请求.我想是否有一个Boto3脚本,它看起来如下所示,但我无法找到它的文档/示例.

示例(伪代码)

table = dynamodb.Table(‘my_table’)
table.BatchWriteItem(RequestItems=file://MyData.json)
Run Code Online (Sandbox Code Playgroud)

参考:http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

指针赞赏.

gjt*_*ton 8

最好看的地方是Boto3的readthedocs:https://boto3.readthedocs.org/en/latest/reference/services/dynamodb.html#DynamoDB.Client.batch_write_item

只要您的JSON格式正确用于请求,您可以使用以下示例:

f = open('MyData.json')
request_items = json.loads(f.read())
client = boto3.client('dynamodb')
response = client.batch_write_item(RequestItems=request_items)
Run Code Online (Sandbox Code Playgroud)