2
0

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-sdk-ruby を v1 から v2 にアップグレードする

Posted at

本家

に書いています。

が、

結構書いていないことが多かったので、書いておきます。
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

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

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?