插入嵌套对象时,Boto3 DynamoDB ParamValidationError

问题描述 投票:0回答:1

我正在尝试将嵌套对象放入(插入)我的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"
      }
   ]
}

我在做什么错?

amazon-web-services amazon-dynamodb boto3 python-3.7
1个回答
0
投票

[放置数组时,

'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'
                }
            }
        ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.