[英]Python - put_item() S3, Lambda, DynamoDB -- An error occurred (ValidationException)
I am using python with the AWS S3, lambda and DynamoDB.我在 AWS S3、lambda 和 DynamoDB 中使用 python。 I have my lambda function set up as a trigger.
我将 lambda 函数设置为触发器。 When I drop a .json file into my S3 bucket, it will activate.
当我将 .json 文件放入我的 S3 存储桶时,它会激活。
When my function activates, it errors once it reaches my put_item function call that is supposed to be storing the json object in my dynamodb table.当我的函数激活时,一旦它到达我的 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
I have tried to change the arguments within the table.put_item(TableName='testTable', Item = jsonDict)
, but the documents I was following before only passed one argument to this function.我试图更改
table.put_item(TableName='testTable', Item = jsonDict)
,但我之前关注的文档仅将一个参数传递给该函数。
Any advice or assistance would be greatly appreciated.任何建议或帮助将不胜感激。
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)
I created my table with the 'test' primary key and the 'test' table name, leaving all of the other settings to default within the AWS dynamo table creation GUI.我使用“test”主键和“test”表名创建了我的表,在 AWS dynamo 表创建 GUI 中将所有其他设置保留为默认值。
{ "test": { "name": "Cody", "age": 27, "car": true } } { "test": { "name": "Cody", "age": 27, "car": true } }
You're trying to use {"name": "Cody", "age": 27, "car": true}
as the value of the primary key.您正在尝试使用
{"name": "Cody", "age": 27, "car": true}
作为主键的值。 In DynamoDB, a primary (partition) key can only be of type string, binary or number .在 DynamoDB 中,主(分区)键只能是string、binary 或 number 类型。
As an example, using {"test": "Cody", "age": 27, "car": True}
as the Item
argument to put_item
would work.例如,使用
{"test": "Cody", "age": 27, "car": True}
作为put_item
的Item
参数会起作用。
Or if you would change the partition key name in the table to name
, calling table.put_item(Item=jsonDict['test'])
would also do the trick.或者,如果您将表中的分区键名称更改为
name
,调用table.put_item(Item=jsonDict['test'])
也可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.