一个或多个参数值无效:预期键 xyz 的类型不匹配:S 实际:M
One or more parameter values were invalid: Type mismatch for key xyz expected: S actual: M
我的 AWS Lambda 根据这个答案调用 DynamoDB:
我收到一个错误:
一个或多个参数值无效:键用户名的类型不匹配预期: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)
}
研究错误导致了一些无法解决上述错误的答案:
我很困惑,因为用户名是一个字符串,我在集成请求映射模板中强制执行它:
#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
boto3 资源 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})
我的 AWS Lambda 根据这个答案调用 DynamoDB:
我收到一个错误:
一个或多个参数值无效:键用户名的类型不匹配预期: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)
}
研究错误导致了一些无法解决上述错误的答案:
我很困惑,因为用户名是一个字符串,我在集成请求映射模板中强制执行它:
#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
boto3 资源 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})