Last active
February 13, 2021 22:43
-
-
Save seveibar/2196c63fcb0b02fe5532d9d2d947cffe to your computer and use it in GitHub Desktop.
Presigning ReceiveMessage and SendMessage SQS Queue Operations in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "httparty" | |
require("aws-sdk") | |
message_body = { "hello": "world" } | |
msg_body_encoded = CGI.escape(message_body.to_json) | |
signer = Aws::Sigv4::Signer.new( | |
service: "sqs", | |
region: "us-east-1", | |
access_key_id: "ACCESS KEY ID", | |
secret_access_key: "SECRET ACCESS KEY" | |
) | |
presigned_url = signer.presign_url( | |
http_method: "get", | |
url: "https://sqs.us-east-1.amazonaws.com/123456789/SomeQueueNmae.fifo/?Action=SendMessage&MessageBody=#{msg_body_encoded}&MessageDeduplicationId=#{dedup_id}&MessageGroupId=Default", | |
expires_in: 1000.day.to_i | |
) | |
response = HTTParty.get(presigned_url, headers: { "Accept": "application/json" }) | |
puts presigned_url | |
puts response | |
# {"SendMessageResponse":{"ResponseMetadata":{"RequestId":"12345678910-100c-510b-96af-12345678910"},"SendMessageResult":{"MD5OfMessageAttributes":null,"MD5OfMessageBody":"fbc24bcc7a1794758fc1327fcfebdaf6","MD5OfMessageSystemAttributes":null,"MessageId":"12345678910-db6c-4e17-b6ac-12345678910","SequenceNumber":"12345678910"}}} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "httparty" | |
require("./config/environment") # We're using a Rails configuration | |
params = { | |
aws_service: "sqs", | |
region: "us-east-1", | |
aws_http_method: "GET", | |
aws_url: "https://sqs.us-east-1.amazonaws.com/123456789/SomeQueueName.fifo?Action=ReceiveMessage" | |
} | |
puts params | |
signer = Aws::Sigv4::Signer.new( | |
service: params[:aws_service], | |
region: Rails.application.config.aws.region, | |
access_key_id: Rails.application.credentials.aws[:access_key_id], | |
secret_access_key: Rails.application.credentials.aws[:secret_access_key] | |
) | |
presigned_url = signer.presign_url( | |
http_method: params[:aws_http_method], | |
url: params[:aws_url], | |
body: nil, | |
expires_in: 1000.day.to_i | |
) | |
puts "\n\n------------------------\n\n" | |
puts presigned_url | |
res = HTTParty.get(presigned_url, headers: { "Accept": "application/json" }) | |
puts "\n\n------------------------\n\n" | |
puts res | |
# {"ReceiveMessageResponse":{"ReceiveMessageResult":{"messages":null},"ResponseMetadata":{"RequestId":"12345678910-a821-5936-9530"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment