我们可以通过消息属性过滤来自 Amazon SQS 队列的消息吗

问题描述 投票:0回答:1

我在此处阅读 我们可以按消息属性过滤 Amazon SQS 队列中的消息吗

我尝试用 python 代码转换它,但它正在获取所有消息。

import boto3
import json

sqs_client = boto3.client('sqs', region_name="us-west-1")

response = sqs_client.create_queue(
    QueueName='test_queue_development'
)
queue_url = response['QueueUrl']

message_body = {'test_data_key': 'test_data_value'}
message_attributes = {
    'method': {
        'DataType': 'String',
        'StringValue': 'DATA'
    }
}
response = sqs_client.send_message(
    QueueUrl=queue_url,
    MessageBody=json.dumps(message_body),
    MessageAttributes=message_attributes
)

message_body = {'test_help_key': 'test_help_value'}
message_attributes = {
    'method': {
        'DataType': 'String',
        'StringValue': 'HELP'
    }
}

response = sqs_client.send_message(
    QueueUrl=queue_url,
    MessageBody=json.dumps(message_body),
    MessageAttributes=message_attributes
)

message_body = {'test_out_of_scope_key': 'test_out_of_scope_value'}
message_attributes = {
    'method': {
        'DataType': 'String',
        'StringValue': 'OUT_OF_SCOPE'
    }
}

response = sqs_client.send_message(
    QueueUrl=queue_url,
    MessageBody=json.dumps(message_body),
    MessageAttributes=message_attributes
)

if 'Messages' in response:
    for message in response['Messages']:
        # Process the message content
        print("Message Body:", message['Body'])

        # Process message attributes
        if 'MessageAttributes' in message:
            for attribute_name, attribute in message['MessageAttributes'].items():
                print(f"Message Attribute - {attribute_name}: {attribute['StringValue']} ({attribute['DataType']})")

        # Delete the processed message from the queue
        receipt_handle = message['ReceiptHandle']
        sqs_client.delete_message(
            QueueUrl=queue_url,
            ReceiptHandle=receipt_handle
        )
else:
    print("No messages received from the queue.")

目标正在根据一些过滤器消费来自SQS队列的消息。它可以基于消息属性或任何其他方式。

注意:在获取消息进行过滤后不考虑应用外部逻辑。

amazon-web-services amazon-sqs
1个回答
0
投票

除非我错过了 SQS 文档中还没有的新内容,否则根据

文档
"FilterPolicy" 不是有效的 SQS 队列属性。

我相信您链接的文章是由 Chat-GPT 等 AI 工具生成的,该工具将 AWS SNS 服务过滤功能 幻觉到 SQS 服务上。

© www.soinside.com 2019 - 2024. All rights reserved.