Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从SNS消息中提取信息_Node.js_Amazon Web Services_Aws Lambda_Amazon Sns - Fatal编程技术网

Node.js 从SNS消息中提取信息

Node.js 从SNS消息中提取信息,node.js,amazon-web-services,aws-lambda,amazon-sns,Node.js,Amazon Web Services,Aws Lambda,Amazon Sns,我正在使用AWS Textract服务从图像中获取文本。过程步骤如下: 将文件上载到S3并输入Dynamodb Dynamodb为Textract触发lambda函数并向SNS发送消息 SNS触发lambda将提取的文本写入Dynamodb表 我目前的问题是在第三步阅读SNS消息。消息如下所示: { Type: 'Notification', MessageId: '00cc3304-a54c-56e4-95c5-746b1579b7a3', TopicArn: 'arn:aws

我正在使用AWS Textract服务从图像中获取文本。过程步骤如下:

将文件上载到S3并输入Dynamodb Dynamodb为Textract触发lambda函数并向SNS发送消息 SNS触发lambda将提取的文本写入Dynamodb表 我目前的问题是在第三步阅读SNS消息。消息如下所示:

  {
  Type: 'Notification',
  MessageId: '00cc3304-a54c-56e4-95c5-746b1579b7a3',
  TopicArn: 'arn:aws:sns:eu-west-1:878512516355:PDF_textprocessing_complete',
  Subject: null,
  Message: '{"JobId":"8ae4a56561be6ddc4e2c8365976f79c","Status":"SUCCEEDED","API":"StartDocumentTextDetection","Timestamp":1593159156962,"DocumentLocation":{"S3ObjectName":"private/eu-west-1:465233397-b3b0-4635-bd4d-249df960b2d6/276bff07-be35-4bf7-bc30-249e86adc3f38invoice_2020_march.png","S3Bucket":"doc9ce275171f09423c586885-dev"}}',
  Timestamp: '2020-06-54T08:18:37.012Z',
  SignatureVersion: '1',
  Signature: 'DdSXJfZvAnsWKnWd+Z7OaeM5MvfcvdzGBP==',
  SigningCertUrl: 'https://sns.eu-west-5.amazonaws.com/SimpleNotificationService-a863b10b4e1f29c941702d737128f7b6.pem',
  UnsubscribeUrl: 'https://sns.eu-west-5.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-5:878519355:PDF_textprocessing_complete:ec5c7a10-2ca-47f7-b1ac-0d0e5fd895618b',
  MessageAttributes: {}
}
拉姆达:

exports.handler = (event)  => {
    const message = event.Records[0].Sns.Message;
    console.log(message)
}
输出如下所示:

  {
  Type: 'Notification',
  MessageId: '00cc3304-a54c-56e4-95c5-746b1579b7a3',
  TopicArn: 'arn:aws:sns:eu-west-1:878512516355:PDF_textprocessing_complete',
  Subject: null,
  Message: '{"JobId":"8ae4a56561be6ddc4e2c8365976f79c","Status":"SUCCEEDED","API":"StartDocumentTextDetection","Timestamp":1593159156962,"DocumentLocation":{"S3ObjectName":"private/eu-west-1:465233397-b3b0-4635-bd4d-249df960b2d6/276bff07-be35-4bf7-bc30-249e86adc3f38invoice_2020_march.png","S3Bucket":"doc9ce275171f09423c586885-dev"}}',
  Timestamp: '2020-06-54T08:18:37.012Z',
  SignatureVersion: '1',
  Signature: 'DdSXJfZvAnsWKnWd+Z7OaeM5MvfcvdzGBP==',
  SigningCertUrl: 'https://sns.eu-west-5.amazonaws.com/SimpleNotificationService-a863b10b4e1f29c941702d737128f7b6.pem',
  UnsubscribeUrl: 'https://sns.eu-west-5.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-5:878519355:PDF_textprocessing_complete:ec5c7a10-2ca-47f7-b1ac-0d0e5fd895618b',
  MessageAttributes: {}
}
如何将JobId和S3ObjectName提取到变量中

非常感谢

您可以使用JSON.parse函数访问消息属性的属性,如下所示

exports.handler = (event)  => {
    const message = event.Records[0].Sns.Message;
    console.log(message);
    const jobid = JSON.parse(message).JobId
    const object = JSON.parse(message).DocumentLocation.S3ObjectName
}
您可以使用JSON.parse函数访问消息属性的属性,如下所示

exports.handler = (event)  => {
    const message = event.Records[0].Sns.Message;
    console.log(message);
    const jobid = JSON.parse(message).JobId
    const object = JSON.parse(message).DocumentLocation.S3ObjectName
}