繁体   English   中英

Python - put_item() S3、Lambda、DynamoDB - 发生错误 (ValidationException)

[英]Python - put_item() S3, Lambda, DynamoDB -- An error occurred (ValidationException)

我在 AWS S3、lambda 和 DynamoDB 中使用 python。 我将 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 } }

您正在尝试使用{"name": "Cody", "age": 27, "car": true}作为主键的值。 在 DynamoDB 中,主(分区)键只能是string、binary 或 number 类型

例如,使用{"test": "Cody", "age": 27, "car": True}作为put_itemItem参数会起作用。

或者,如果您将表中的分区键名称更改为name ,调用table.put_item(Item=jsonDict['test'])也可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM