amazonica icon indicating copy to clipboard operation
amazonica copied to clipboard

SQS send-message with message-attributes

Open ctjhoa opened this issue 4 years ago • 3 comments

Hi, I try to send an SQS message with a message-attributes. I notice there is no coercion like SNS ( https://github.com/mcohen01/amazonica/pull/179 ) So my code is:

(sqs/send-message {:queue-url queue-url
                   :message-body empty-message-body
                   :message-attributes {(str my-attribute-key) (doto
                                                                 (MessageAttributeValue.)
                                                                   (.withDataType "String")
                                                                   (.withStringValue (str my-attribute-value)))}})

However I've got this exception:

Error setting :message-attributes:
Failed to create an instance of com.amazonaws.services.sqs.model.MessageAttributeValue from
{\"my-attribute-key\" #object[com.amazonaws.services.sqs.model.MessageAttributeValue 0x4762c694 \"{StringValue: my-attribute-value,StringListValues: [],BinaryListValues: [],DataType: String}\"]}
due to java.lang.IllegalArgumentException:
Don't know how to create ISeq from: com.amazonaws.services.sqs.model.MessageAttributeValue.
Make sure the data matches an existing constructor and setters..
Perhaps the value isn't compatible with the setter?

As there is no documentation nor example on this case, can you tell me if I miss something or if I have a bad send-message usage? Thanks

ctjhoa avatar Nov 02 '20 13:11 ctjhoa

Ok I found my answer on a slack archive https://clojurians-log.clojureverse.org/aws/2017-01-20

So you should do something like this:

(sqs/send-message {:queue-url a
                   :message-body "hello world!"
                   :message-attributes {:attribute-key
                                                      {:string-value attribute-value
                                                       :data-type "String"
                                                       :string-list-values []
                                                       :binary-list-values []}})

I don't close this issue as I think the interface between SNS & SQS isn't coherent.

(publish :topic-arn "arn:aws:sns:us-east-1:676820690883:my-topic"
         :subject "test"
         :message (str "Todays is " (java.util.Date.))
         :message-attributes {attribute-key attribute-value})

IMO amazonica should allow to do something like this

(sqs/send-message {:queue-url a
                   :message-body "hello world!"
                   :message-attributes {attribute-key attribute-value}})

as Request objects are comparable

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sqs/model/SendMessageRequest.html#setMessageAttributes-java.util.Map- https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sns/model/PublishRequest.html#setMessageAttributes-java.util.Map-

ctjhoa avatar Nov 04 '20 09:11 ctjhoa

Top level service functions take clojure data as arguments, the form of which is governed by the recursive conversion of camel case Java objects to hyphenated clojure data. What do you see as inconsistent here between sqs/send-message and sns/publish? Both are invoked the same way and their arguments coerced to Java objects the same way.

mcohen01 avatar Nov 06 '20 15:11 mcohen01

@mcohen01

Both are invoked the same way and their arguments coerced to Java objects the same way.

They don't coerced the same way. SNS has coercion https://github.com/mcohen01/amazonica/pull/179/files which isn't available in SQS. This is confusing as only SNS is mentioned in the documentation. I'll create a PR to add the same coercion mechanism for SQS.

ctjhoa avatar Nov 06 '20 17:11 ctjhoa