如何使用 boto3 手动从 DynamoDB 有线协议转换为本机 Python 对象? [英] How to convert from DynamoDB wire protocol to native Python object manually with boto3?

查看:25
本文介绍了如何使用 boto3 手动从 DynamoDB 有线协议转换为本机 Python 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 DynamoDB 流触发的 Lambda.Lambda 进行一些处理,然后在 SNS 中创建有关主题的通知.理想情况下,我希望将整个新文档包含在发送到 SNS 的通知中,这样下游客户端就不必点击 DynamoDB 来获取数据.

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 include the entire new document in the notification that goes out to SNS so that downstream clients don't have to hit DynamoDB to get the data.

我遇到的问题是来自 DynamoDB 流的数据采用 DynamoDB 有线格式(映射包括数据类型作为键).当我向下游客户端发送通知时,我不希望他们必须了解 DynamoDB 有线格式来解析消息(例如,如果我切换到新的底层数据存储,我将不得不重新创建该格式).

The problem I'm running into is that the data coming from the DynamoDB stream is in DynamoDB wire format (the maps include the data type as a key). When I send out the notification to downstream clients I don't want them to have to understand DynamoDB wire format to parse the message (for example if I switch to a new underlying data store I would then have to recreate that format).

显然 boto3 客户端能够将此格式解析为 Python 对象,有没有办法让我自己访问解析器?据我所知,它是作为从 DynamoDB 获取数据的一部分被调用的,但我找不到自己调用它的方法.

Obviously the boto3 client is capable of parsing this format into a Python object, is there a way for me to access the parser on my own? As far as I can tell it gets called as part of fetching data from DynamoDB but I can't find a way to call it on my own.

推荐答案

我也有类似的情况,我用了如下这样的方法:

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])

这种方法对我有用.结果字典 d 包含转换后的对象,而不是传递给处理程序的有线格式版本.

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

这篇关于如何使用 boto3 手动从 DynamoDB 有线协议转换为本机 Python 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆