AWSSNS过滤为何不忽略未知消息属性?
创始人
2024-09-27 11:30:41
0

在 AWS SNS 过滤中,未知消息属性不会被忽略,这意味着您需要为所有想要匹配的属性都创建一个过滤策略。要解决这个问题,您可以通过在过滤筛选器中添加适配器来忽略未知属性。以下是一个示例代码,用于在 Node.js 和 Typescript 中过滤 SNS 消息:

import * as AWS from 'aws-sdk';
import * as SNS from "aws-sdk/clients/sns";

AWS.config.update({ region: 'us-east-1' });
const sns: SNS = new AWS.SNS({ apiVersion: '2010-03-31' });

const subscriptions: any[] = [];

function subscribeToTopic(topicArn: string, filterPolicy: object) {
   const params = {
      Protocol: 'https',
      TopicArn: topicArn,
      Attributes: {
         'FilterPolicy': JSON.stringify(filterPolicy)
      }
   };

   sns.subscribe(params, function (err, data) {
      if (err) {
         console.error(err);
      } else {
         subscriptions.push(data.SubscriptionArn);
         console.log('Subscribed to SNS topic successfully. Subscription ARN: ' + data.SubscriptionArn);
      }
   });
}

function handleSNSMessage(event: any) {
   const snsMessage = event.Records[0].Sns;
   console.log('Received SNS message: ' + JSON.stringify(snsMessage.Message));

   // Check for known message attributes
   if (snsMessage.MessageAttributes['attribute1']) {
      // Process the message
   }
   if (snsMessage.MessageAttributes['attribute2']) {
      // Process the message
   }
}

// Subscribe to the SNS Topic
const topicArn = 'arn:aws:sns:us-east-1:123456789:mytopic';
const filterPolicy = {
   "attribute1": ["value1", "value2"],
   "attribute2": "value3"
};
subscribeToTopic(topicArn, filterPolicy);

// Listen to the SNS notifications
export const snsNotificationHandler = async (event: any) => {
   console.log('Received SNS notification.');
   await handleSNSMessage(event);
};

在此示例代码中,我们首先定义了 subscribeToTopic() 函数,该函数将在 SNS Topic 上创建一个订阅并添加筛选策略。然后,我们使用 handleSNSMessage() 函数来处理接收到的消息,该函数检查 MessageAttributes 是否有已知

相关内容

热门资讯

wepokeai机器人(透视)... wepokeai机器人(透视)wepoke有没有玄学(详细辅助新2025版)确实是有挂(可靠辅助德之...
wepoke模拟器(透视)we... wepoke模拟器(透视)wepoke软件透明功能实现方法(详细辅助教你教程)好像是真的有挂(科普插...
wepoke辅助德之星(透视)... wepoke辅助德之星(透视)wepooke app系统规律(详细辅助攻略方法)真是是有挂(教你有辅...
wepoke有辅助挂(透视)w... wepoke有辅助挂(透视)wepoke有辅助挂吗(详细辅助科技教程)一直是有挂(教你透视挂)1、让...
wepoke辅助有挂(透视)w... wepoke辅助有挂(透视)wepoke人有挂吗(详细辅助可靠技巧)其实有挂(了解辅助)1.wepo...
wepoke插件(透视)wep... wepoke插件(透视)wepoke软件收费是真的吗(详细辅助辅助教程)其实真的是有挂(专业模拟器)...
wepoke辅助德之星(透视)... wepoke辅助德之星(透视)wopoker辅助(详细辅助新2025教程)真是真的有挂(玩家ai机器...
we辅助poker德之星(透视... we辅助poker德之星(透视)wepoke辅助挂在哪开(详细辅助新2025版)确实是真的有挂(大神...
wepoke黑科技(透视)we... wepoke黑科技(透视)wepoke挂真的假的(详细辅助揭秘攻略)竟然存在有挂(攻略计算辅助)1、...
wepoke智能ai(透视)w... wepoke智能ai(透视)wepower让系统发好牌(详细辅助教你攻略)真是真的有挂(详细有挂)1...