如何获取aws sqs客户端并将消息发送到队列?

时间:2017-09-19 09:33:01

标签: ruby aws-sdk amazon-sqs

如何获取aws sqs客户端并将消息发送到ruby sdk中的队列?

$sqs = Aws::SQS::Client.new(
  :access_key_id => SQS_CONFIG[:access_key_id], 
  :secret_access_key => SQS_CONFIG[:secret_access_key], 
  :region => 'us-east-1'
)

之后如何将消息推送到队列?

1 个答案:

答案 0 :(得分:2)

来自AWS Developer Guide

require 'aws-sdk'

sqs = Aws::SQS::Client.new(region: 'us-east-1')

# Send a message to a queue.
queue_name = "my-queue"

begin
  queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url

  # Create a message with three custom attributes: Title, Author, and WeeksOn.
  send_message_result = sqs.send_message({
    queue_url: queue_url, 
    message_body: "Information about current NY Times fiction bestseller for week of 2016-12-11.",
    message_attributes: {
      "Title" => {
        string_value: "The Whistler",
        data_type: "String"
      },
      "Author" => {
        string_value: "John Grisham",
        data_type: "String"
      },
      "WeeksOn" => {
        string_value: "6",
        data_type: "Number"
      }
    }
  })
rescue Aws::SQS::Errors::NonExistentQueue
  puts "A queue named '#{queue_name}' does not exist."
  exit(false)
end

puts send_message_result.message_id