一个或多个参数值无效:键 xyz 的类型不匹配预期:S 实际:M

2024-04-30

我的 AWS Lambda 根据以下答案调用 DynamoDB:https://stackoverflow.com/a/33649402/495455 https://stackoverflow.com/a/33649402/495455

我收到错误:

一个或多个参数值无效:键的类型不匹配 用户名 预期:S 实际:M

这是 python 代码,但 JavaScript 等也会出现错误:

import json
import boto3
client = boto3.resource('dynamodb')
table = client.Table("DS-Users")

def lambda_handler(event, context):

    UserName = event['UserName']
    Email = event['Email']
    Score = event['Score']
    Mobile = event['Mobile']
    CountryId = event['CountryId']
    Level = event['Level']
    Magic = event['Magic']

    table.put_item(Item={'UserName':{'S':UserName},'Email':{'S':Email},'Score':{'N':Score},'Level':{'N':Level},'Mobile':{'S':Mobile},'Magic':{'S':Magic}})

    return {
        'statusCode': 200,
        'body': json.dumps(event)
    }

研究错误会产生一些无法解决上述错误的答案:

  1. https://github.com/awslabs/dynamodb-document-js-sdk/issues/17 https://github.com/awslabs/dynamodb-document-js-sdk/issues/17

  2. https://forums.aws.amazon.com/thread.jspa?threadID=248424 https://forums.aws.amazon.com/thread.jspa?threadID=248424

我很困惑,因为 UserName 是一个字符串,我在集成请求映射模板中强制执行它:

#set($inputRoot = $input.path('$'))
{
  "UserName" : "$inputRoot.UserName",

有任何想法吗?为什么它将字符串视为模型数据类型?


首先注意AWS SDK V1与V2的版本!

另请注意 boto3 的客户端与资源 API 的语法。{'UserName':{'S':UserName}是用于 boto3 客户端的语法,此解决方案适用于 boto3 资源变体。

boto3 客户端 put_item 参考 –https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

boto3 资源 put_item 参考 -https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.put_item https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.put_item

客户端语法

{'UserName':{'S':UserName},

资源语法

{'UserName':UserName,

对于我的具体情况,这解决了它:

table.put_item(Item={'UserName':UserName,'Email':Email,'Score':Score,'Level':Level,'Mobile':Mobile,'Magic':Magic})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一个或多个参数值无效:键 xyz 的类型不匹配预期:S 实际:M 的相关文章

随机推荐