How to convert from DynamoDB wire protocol to native Python object manually with boto3?

后端 未结 2 729
灰色年华
灰色年华 2021-02-08 15:36

I have a Lambda that is being triggered by a DynamoDB stream. The Lambda does some processing and then creates a notification on a topic in SNS. Ideally I would like to includ

相关标签:
2条回答
  • 2021-02-08 15:53

    Using TypeDeserializer didn't work for me for some reason. This utility did, however:

    https://github.com/Alonreznik/dynamodb-json

    Those not wanting to deal with emulating pip install inside a Lambda function, you can just copy and paste this source file (containing the loads() function) into your own code:

    https://github.com/Alonreznik/dynamodb-json/blob/master/dynamodb_json/json_util.py

    0 讨论(0)
  • 2021-02-08 16:07

    I have a similar situation and I used the following an approach like this:

    from boto3.dynamodb.types import TypeDeserializer
    
    deser = TypeDeserializer()
    
    ...
    <in handler>
        for record in event['Records']:
            old = record['dynamodb'].get('OldImage')
            new = record['dynamodb'].get('NewImage')
            if old:
                d = {}
                for key in old:
                    d[key] = deser.deserialize(old[key])
    

    This approach works for me. The resulting dictionary d contains the converted object rather than the wire-format version passed to the handler.

    0 讨论(0)
提交回复
热议问题