6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【AWS】AWS SDK for Ruby V2でSQSを感じてみる

Last updated at Posted at 2015-06-24

どうも、千葉です。
AWS SDK for Ruby v2で、SQSのキューを感じて、メッセージを出力するコードをメモしておきます。
V1から、かなり変わってるので注意が必要ですね。

アクセスキーの設定は、別途設定必要です。

sqs_receive.rb
#!/usr/bin/ruby

require 'aws-sdk'
require 'json'

# キューの設定
queue_url = "https://sqs.ap-northeast-1.amazonaws.com/XXXXX/XXXXX"
sqs = Aws::SQS::Client.new(region: 'ap-northeast-1')

loop {
    # メッセージの取得
    msg = sqs.receive_message({
        queue_url: queue_url
    })

    unless msg.messages[0].nil?
        # メッセージの表示
        result = JSON.parse(msg.messages[0].body)
        puts result["Message"]

        # メッセージ削除
        resp = sqs.delete_message({
            queue_url: queue_url, # required
            receipt_handle: msg.messages[0].receipt_handle # required
       })
    end

    msg = nil
    sleep 10
}

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?