Search code examples
pythonamazon-web-servicesamazon-sqs

Response from AWS SQS Java SDK does not match Python SDK or CLI


We are using Java to send/receive messages from SNS/SQS. When we publish the message, we are doing this:

final var req = PublishRequest.builder()
    .topicArn(topicArn)
    .message(payload)
    .subject(subject)
    .build();
final var response = snsClient.publish(req);

Note that we are setting subject here -- this is important as we use it to drive logic on the receiving end. When we do receive messages, we parse the JSON to an object and correctly end up with a subject. However, we are now using the Python SDK for to receive message as well, and in that case there is no "subject" attribute sent back from the API call:

queue_name = "..."
sqs = boto3.resource("sqs")
queue = sqs.get_queue_by_name(QueueName=queue_name)
messages = queue.receive_messages(
    MaxNumberOfMessages=1,
    WaitTimeSeconds=10,
    AttributeNames=["All"],
    MessageAttributeNames=["All"],
)

for message in messages:
    print("Got message:")
    print(f"Queue URL: {message.queue_url}")
    
    if message.attributes != None:
        print(f"Attributes:\n{json.dumps(message.attributes, indent=2)}")
    
    if message.message_attributes != None:
        print(f"Message Attributes:\n{json.dumps(message.message_attributes, indent=2)}")
    
    print(f"Body:\n{json.dumps(json.loads(message.body), indent=2)}")

Results in:

Got message:
Queue URL: https://...
Attributes:
{
  "SenderId": "...",
  "ApproximateFirstReceiveTimestamp": "1737570929119",
  "ApproximateReceiveCount": "5",
  "SentTimestamp": "1737535740136"
}
Body:
{
   ...
}

Note that there is no top-level "subject" attribute, and the https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs/queue/receive_messages.html does not mention "subject" at all.


Am I missing something or is the output of the two SDKs simply not the same? Note that I've also tried using the SQS Client (rather than the "resource") in Python. How can I get the Python SDK to include the "subject"?

I've also configured an email subscription and the subject shows up in the email subject as well in the JSON body of the email, as a standalone property.

Java AWS SDK: 2.29.29

Python: 3.10.2

Python AWS SDK: 1.33.13


Solution

  • I resolved this by turning off the "raw message delivery" option on the SNS subscription. That option, when enabled, tells SNS to not include the event info, just the payload.