【问题标题】:Python - put_item() S3, Lambda, DynamoDB -- An error occurred (ValidationException)Python - put_item() S3、Lambda、DynamoDB -- 发生错误 (ValidationException)
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 我重新格式化了我的 JSON 以包含 test 的主键,就像你说的那样。这非常有效。谢谢米兰。
  • 是的,我很高兴它有帮助。随意点赞并接受答案,以便其他人也能认为它有帮助。
  • 我还需要两个声望,然后我就可以开始投票了。一旦我得到它,我会立即支持你的答案。我保证。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
相关资源
最近更新 更多