Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

Organization

aws-sdk-ruby を v1 から v2 にアップグレードする

本家

に書いています。

が、

結構書いていないことが多かったので、書いておきます。
SQSとS3にフォーカスします。

本家にも書いていること

s3 = AWS::S3::Client.new # version 1
s3 = Aws::S3::Client.new # version 2

# version 1
s3 = AWS::S3.new

# version 2
s3 = Aws::S3::Resource.new

# version 1
s3.buckets['bucket-name'].objects['key'].delete

# version 2
s3.bucket('bucket-name').object('key').delete

設定系も変わっています。

# version 1
AWS.config({
  access_key_id: 'xxxxx',
  secret_access_key: 'xxxxxxx',
  region: 'ap-northeast-1'
})

# version 2
# config は hash になった
Aws.config.update({
  access_key_id: 'xxxxx',
  secret_access_key: 'xxxxxxx',
  region: 'ap-northeast-1'
})

書いていないこと(S3)

#--------------------------
# version 1
#--------------------------
bucket = AWS::S3.new.buckets['bucket-name']

s3object = bucket.objects['key']

# download
File.open('output', 'wb') do |f|
  s3object.read do |chunk|
    f.write(chunk)
  end
end

# data
data = s3object.read

# url
p bucket.url # "https://bucket-name.s3-ap-northeast-1.amazonaws.com"

# prefix 指定の一括削除
bucket.objects.with_prefix("prefix").delete_all

#--------------------------
# version 2
#--------------------------
bucket = Aws::S3::Resource.new.bucket('bucket-name') # Resouce ついて、bucket は単数形、[] => ()

s3object = bucket.object('key') # object は単数形、[] => ()

# download
File.open('output', 'wb') do |f|
  s3object.get do |chunk|
    f.write(chunk)
  end
end

# data
data = s3object.get.body.read

# url
p bucket.url # "http://bucket-name.s3.amazonaws.com/" 末尾にslashがつく

# prefix 指定の一括削除
bucket.objects({prefix: "prefix"}).batch_delete!

書いていないこと(SQS)

#--------------------------
# version 1
#--------------------------
sqs_queue = AWS::SQS.new.queues.named('queue_name')
sqs_queue.poll do |msg|
  # polling
  p msg.id
end


#--------------------------
# version 2
#--------------------------
client = Aws::SQS::Client.new
result = client.get_queue_url({ queue_name: 'queue_name' })
poller = Aws::SQS::QueuePoller.new(result.queue_url)

poller.poll do |msg|
  # polling
  p msg.message_id
end

もっといいやりかたがあるのかも。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?