【发布时间】:2019-12-27 08:57:29
【问题描述】:
我将 python 与 AWS S3、lambda 和 DynamoDB 一起使用。我将我的 lambda 函数设置为触发器。当我将 .json 文件放入我的 S3 存储桶时,它将激活。
当我的函数激活时,它会在到达我的 put_item 函数调用时出错,该函数调用应该将 json 对象存储在我的 dynamodb 表中。
错误文字:
[ERROR] ClientError: An error occurred (ValidationException)
when calling the PutItem operation: One or more parameter values
were invalid: Missing the key test in the item
我曾尝试更改table.put_item(TableName='testTable', Item = jsonDict) 中的参数,但我之前关注的文档仅将一个参数传递给此函数。
任何建议或帮助将不胜感激。
我的代码指出:
import boto3
import json
s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
json_file_name = event['Records'][0]['s3']['object']['key']
json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name)
jsonFileReader = json_object['Body'].read()
jsonDict = json.loads(jsonFileReader)
table = dynamodb.Table('test')
table.put_item(Item = jsonDict)
return 'Hello from lambda'
云观察日志:
[ERROR] ClientError: An error occurred (ValidationException) when
calling the PutItem operation: One or more parameter values were
invalid: Missing the key test in the item
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 14, in lambda_handler
response = table.put_item(Item = jsonDict)
File "/var/runtime/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/var/runtime/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/var/runtime/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 623, in _make_api_call
raise error_class(parsed_response, operation_name)
编辑:
我使用“test”主键和“test”表名创建了我的表,在 AWS dynamo 表创建 GUI 中将所有其他设置保留为默认设置。
json文件内容:
{ "test": { "name": "Cody", "age": 27, "car": true } }
【问题讨论】:
-
错误是说您放入 DynamoDB 的 JSON 没有
test的键。请编辑您的问题以显示 DynamoDB 表的设计(它是否具有test的哈希键?)以及您正在加载到jsonDict的数据示例。
标签: python amazon-web-services aws-lambda boto3