Amazon Cognito Streams 在 Lambda 事件中提供垃圾数据

Amazon Cognito Streams gives garbage data in the Lambda events

我已将 Lambda 配置为从 Cognito Sync 写入的 Kinesis Stream 中读取。对于每个调用的 CognitoSync,我都会在 Lambda 日志中看到一个事件,但该事件不包含我在数据集中设置的数据键值。我确实在请求中看到键值被发送到 Cognito。

Lambda 获取的事件如下所示。如何从中获取数据集中的键值。

2015-03-07T16:18:40.082Z    9be3582e-c4e5-11e4-be53-6f01632e7b6d
{
  "Records": [
    {
      "eventSource": "aws:kinesis",
      "kinesis": {
        "partitionKey": "us-east-1:d4bfff5d-9605-484d-9aab-0e63829b1e54-Fia",
        "kinesisSchemaVersion": "1.0",
        "data": "eyJpZGVudGl0eVBvb2xJZCI6InVzLWVhc3QtMTowMmFiM2JiYi04N2RlLTQyMzUtYWEyZS1kNzliYzQ1YmFmOTciLCJpZGVudGl0eUlkIjoidXMtZWFzdC0xOmQ0YmZmZjVkLTk2MDUtNDg0ZC05YWFiLTBlNjM4MjliMWU1NCIsImRhdGFzZXROYW1lIjoiRmlhciIsIm9wZXJhdGlvbiI6InJlcGxhY2UiLCJwYXlsb2FkVHlwZSI6IklubGluZSIsImtpbmVzaXNTeW5jUmVjb3JkcyI6W3sia2V5IjoiU3RhdGUiLCJ2YWx1ZSI6IltbXCItXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCJdLFtcIi1cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIl0sW1wiT1wiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiXSxbXCJYXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCIsXCItXCJdLFtcIk9cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIixcIi1cIl0sW1wiWFwiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiLFwiLVwiXV0iLCJzeW5jQ291bnQiOjYsImxhc3RNb2RpZmllZERhdGUiOjE0MjU3NDUxMTQ3NjMsImRldmljZUxhc3RNb2RpZmllZERhdGUiOjE0MjU3NDUxMTE0NDAsIm9wIjoicmVwbGFjZSJ9XSwia2luZXNpc1N5bmNSZWNvcmRzVVJMIjpudWxsLCJsYXN0TW9kaWZpZWREYXRlIjoxNDI1NzQ1MTE0NzYzLCJzeW5jQ291bnQiOjZ9",
        "sequenceNumber": "49548516359756600751834810213344902796782628138546888706"
      },
      "eventID": "shardId-000000000000:49548516359756600751834810213344902796782628138546888706",
      "invokeIdentityArn": "arn:aws:iam::111111111111:role/LambdaKinesisInvocationRole-funcog",
      "eventName": "aws:kinesis:record",
      "eventVersion": "1.0",
      "eventSourceARN": "arn:aws:kinesis:us-east-1:111111111111:stream/funcog",
      "awsRegion": "us-east-1"
    }
  ]
}

您从 Kinesis(在 .Records[0].kinesis.data 元素中)获得的数据似乎是 Base64-encoded。解码给出以下内容:

{"identityPoolId":"us-east-1:02ab3bbb-87de-4235-aa2e-d79bc45baf97","identityId":"us-east-1:d4bfff5d-9605-484d-9aab-0e63829b1e54","datasetName":"Fiar","operation":"replace","payloadType":"Inline","kinesisSyncRecords":[{"key":"State","value":"[[\"-\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"],[\"-\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"],[\"O\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"],[\"X\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"],[\"O\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"],[\"X\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"]]","syncCount":6,"lastModifiedDate":1425745114763,"deviceLastModifiedDate":1425745111440,"op":"replace"}],"kinesisSyncRecordsURL":null,"lastModifiedDate":1425745114763,"syncCount":6}

所以在您的 Lambda 函数中,您需要解析这些数据。一种方法可能如下所示:

var data = JSON.parse(new Buffer(event.Records[0].kinesis.data, 'base64').toString('ascii'));
console.log("Key: " + data.kinesisSyncRecords[0].key);
// etc...