Unmarshalling Dynamo DB stream data to Json format

青春壹個敷衍的年華 提交于 2019-12-09 02:19:29

I am able to resolve it by

var s = JSON.stringify(data.NewImage);
   s =  s.replace('"B":', '"S":');
   parserd_json = JSON.parse(s);
   unmarshalleddata = aws.DynamoDB.Converter.unmarshall(parserd_json);

If any one has any better solution. Please suggest.

The issue appears to be that the unmarshall function expects binary B values to already be a Buffer instance for some reason. If it is a string what it actually does is encodes the string as a Buffer containing the string's utf8 bytes.

To work around it in my project what I did was:

npm install deep-for-each
import * as deepForEach from 'deep-for-each';
import { DynamoDB } from 'aws-sdk';

export function eventImageUnmarhall(data: DynamoDB.AttributeMap): any {

    deepForEach(data, replaceBinaryBase64WithBuffer);
    return DynamoDB.Converter.unmarshall(data);
}

function replaceBinaryBase64WithBuffer(value: any, key: string | number, subject: any) {
    if (key === 'B' && typeof value === "string") {
        subject[key] = Buffer.from(<string>value, 'base64');
    }
}

Then you use it like this:

import {eventImageUnmarhall} from './dynamo-helpers';

//
//…
//

let unmarshalledData = eventImageUnmarhall(data.NewImage);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!