创建 SNS 主题和 SQS 队列,并将其绑定。
在 SNS 中设置订阅过滤策略。
例如,使用以下 JSON 格式的订阅过滤策略:
{
"FilterPolicy": {
"company": {
"anything-but": "ABC Inc."
},
"price": [
{
"numeric": [
">=",
20
]
},
{
"numeric": [
"<=",
50
]
}
]
}
}
以上格式将根据以下条件过滤消息:
例如,使用以下 Python 代码发布消息:
import json
import boto3
client = boto3.client('sns')
message = {'company': 'XYZ Corp.', 'price': 25}
response = client.publish(TopicArn='arn:aws:sns:us-west-2:123456789012:my-topic', Message=json.dumps(message))
例如,使用以下 Python 代码接收消息:
import boto3
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='my-queue')
for message in queue.receive_messages():
print(message.body)
message.delete()
以上代码将接收符合订阅过滤策略的消息并将其从队列中删除。
上一篇:AWSSNSSQS-订阅过滤策略