gpt4 book ai didi

amazon-web-services - Boto3 DynamoDB ParamValidationError 插入嵌套对象时

转载 作者:行者123 更新时间:2023-12-04 01:38:21 28 4
gpt4 key购买 nike

我正在尝试将嵌套对象放入(插入)我的 DynamoDB 表中。我根据 boto3 文档构建的对象是:

itm = {
'uid': {
'S': 'some-unique-id-value'
},
'myArray': {
'L': [
{
'propOne': {
'S': 'this is value of prop 1'
},
'createdOn': {
'S': str(time.time())
},
'isActive': {
'BOOL': True
},
'propTwo': {
'S': 'this is value of prop 2'
},
'propThree': {
'S': 'this is value of prop 3'
}
}
]
}
}

然后我使用 dynamoDB client 放置项目:

dynamodb.put_item(TableName='myTableName', Item=itm)

但我遇到了异常:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Unknown parameter in Item.myArray.L[0]: "propOne", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "createdOn", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "isActive", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "propTwo", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL
Unknown parameter in Item.myArray.L[0]: "propThree", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL

我正在关注此文档:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

我的目标是存储此 JSON 对象中所示的项目:

{
"uid":"some-uid-val",
"myArray":[
{
"propOne":"item 1 prop 1 value",
"createdOn":"123456",
"isActive":true,
"propTwo":"item 1 prop 2 value",
"propThree":"item 1 prop 3 value"
},
{
"propOne":"item 2 prop 1 value",
"createdOn":"123456",
"isActive":true,
"propTwo":"item 2 prop 2 value",
"propThree":"item 2 prop 3 value"
}
]
}

我做错了什么?

最佳答案

当你放置一个数组时,

'L': [
{'... recursive ...'},
],

它需要一个项目结构的递归。由于您有 dict 类型的列表项,因此列表中有嵌套的 dict 对象。然后,您的项目应如下所示:

itm = {
'uid': {
'S': 'some-unique-id-value'
},
'myArray': {
'L': [
'M': {
'propOne': {
'S': 'this is value of prop 1'
}
},
'M': {
'createdOn': {
'S': str(time.time())
}
},
'M': {
'isActive': {
'BOOL': True
}
},
'M': {
'propTwo': {
'S': 'this is value of prop 2'
}
},
'M': {
'propThree': {
'S': 'this is value of prop 3'
}
}
]
}
}

关于amazon-web-services - Boto3 DynamoDB ParamValidationError 插入嵌套对象时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58572744/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com