I have to translate the DDB stream message into normal json type. For this I am using
unmarshalleddata = aws.DynamoDB.Converter.unmarshall(result.NewImage);
where result.NewImage is
{ carrier: { S: 'SPRING' },
partnerTransactionId: { S: 'a87ce47a46d7416586e0ece39f706d48' },
shipmentId: { S: 'SPRING2200419561404932' },
compressedShipmentPayload:
{ B: 'H4sIAAAAAAAAAO1b+ZPiRrL+Vzr4tb3TkrgaR7yIFWdDI3FJHHqxMSEkIQQ6aB1cjvnfX2ZVAoJur+1963hHeMaepirry8r6MisrRZd+KVhRHDu+mXpR2LULPxeWZkkwq2ZVFEulklWumo65sqt2tVhbrZzqqlL4qWBlfprFTiOyHQA44d86dejdmXEaOjEqqdSqr+WyUKpde7XYDBPTus5ivlYtp1Q1SxW7WhIr5deKIziWU6xZVaFil15BX+KlDhu7tS2c1Ixjz4mhPRmOu2oHR6y9XeCEadOLHaYahFGWLqMstG+IoXnCQd1wFRV+/s9fCulp5wxWjbUZu2i+6fv8c0JrOCEJnu9PnNCG+X4qmJYFGlM1C5ZsflGAP8WSgEbuoiQ1fWIC58SR8enS8eMfNytl246dJGE2JKckdYKpE3srz4E1hpnvw0R8hJ6YzDIEtuMoYGrDJI0zK52avmf3zSTteyGMWZl+4vxUSM1j19ZgXRdNrONi8J3yws+/gLZgZ4a4TlTfiGCC0Awcaj+p+BmWto5wikKpJAn8L3Q6gen50LmCgX/Hf6zoG/yXbQvXGdAyXGWh/KTvdk78NNGeFODVC5OnvhmiajYNjhMvjUkG3pYKSBd+uhi9C5aDWEmsSzvJlg0PPcSbFnzWogP6vR+FNgQADEnN1BnG0d4LLXLKnZNmDemt1pLvulvH1AkTFkCk+M6NLLypi5FDo2C1ng2e9Uy/6fje3olP3dD2LDONrrRTEI4hLq84mwYPIy9ML52BmVrrmw9//PjpD4SJFv2JQSL7tgcOsm9hokWPQVIVK6JULJUr1VyQpJEJ0CSz/379BMESfBEqHd/Mllnsgv3fnko1GAFTXAIEpv7vREYbUs92lcXp7wiOilAUX/9IaDRb/5tD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1l2GjeTvjfFgMvleH4ybrfH3Zmv6XWtNtDwSz1L2Skvq3WFvL+88CbWfhTLE7zfoYqu8eh9X+A88Ic6BmYLHr+9n/fgvWEnzih88AAA=' },
correlationId: { S: 'ba40a7a721444c57aeafd7d739ffe7f6' },
partnerId: { S: '697855049' },
trackingNumber: { S: '3SDMZR6920882' },
ttl: { N: '1538125523' },
createDate: { S: '2018-09-21 09:05:23' } }
And the unmarshalled stream is
{ carrier: 'SPRING',
partnerTransactionId: 'a87ce47a46d7416586e0ece39c706d48',
shipmentId: 'SPRING2200419561444932',
compressedShipmentPayload: <Buffer 48 34 73 49 41 41 41 41 41 41 41 41 41 4f 31 62 2b 5a 50 69 52 72 4c 2b 56 7a 72 34 74 62 33 54 6b 72 67 61 52 37 79 49 46 57 64 44 49 33 46 4a 48 48 ... >,
correlationId: 'ba40a7a711444c57aeafd7d739ffe7f6',
partnerId: '697855049',
trackingNumber: '3SDMZR6920881',
ttl: 1538125523,
createDate: '2018-09-21 09:05:23' }
Everything is correct except the compressedShipmentPayload which is of type B dynamo attribute type ( actually a compressed GZIP text via dyamodb compression utility only) . After unmarshalling, its getting changed to Unit8ArrayBuffer. How can I handle this not to be converted to Unit8ArrayBuffer but remain as the compressed text type only.
I have also tried :
console.log(aws.DynamoDB.Converter.output({ 'M': result.NewImage }));
and
var docClient = new aws.DynamoDB.DocumentClient()
//Create a Translator object, which comes from the DocumentClient
var dynamodbTranslator = docClient.getTranslator();
var ItemShape = docClient.service.api.operations.getItem.output.members.Item;
result.NewImage = dynamodbTranslator.translateOutput(result.NewImage, ItemShape);
console.log(result.NewImage)
But all leading to same results.
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);
来源:https://stackoverflow.com/questions/52510903/unmarshalling-dynamo-db-stream-data-to-json-format