174
154

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 for Ruby V2でS3をいろいろ使う

Last updated at Posted at 2016-05-31

インストール

gem install aws-sdk

バケット

Aws::S3::Clientを使う場合

client作成

require 'aws-sdk'
client = Aws::S3::Client.new(
                             :region => 'region',
                             :access_key_id => 'xxxxxx',
                             :secret_access_key => 'xxxxxx',
                             )

バケット作成

client.create_bucket(:bucket => 'bucket_name')

バケット一覧

client.list_buckets.buckets.map(&:name)
#=> ['bucket1', 'bucket2', 'bucket3']

バケット削除

client.delete_bucket(:bucket => 'bucket_name')

Aws::S3::Resourceを使う場合

resource作成

require 'aws-sdk'
res = Aws::S3::Resource.new(
                             :region => 'region',
                             :access_key_id => 'xxxxxx',
                             :secret_access_key => 'xxxxxx',
                             )

バケット作成

res.create_bucket(:bucket => 'bucket_name')

バケット存在チェック

res.bucket('bucket_name').exists?
#=> true or false

バケット一覧

res.buckets.map(&:name)
#=> ['bucket1', 'bucket2', 'bucket3']

バケット削除

res.bucket('bucket_name').delete

オブジェクト

Aws::S3::Clientを使う場合

client作成

require 'aws-sdk'
client = Aws::S3::Client.new(
                             :region => 'region',
                             :access_key_id => 'xxxxxx',
                             :secret_access_key => 'xxxxxx',
                             )

アップロード

client.put_object(
                  :bucket => 'bucket_name',
                  :key    => 'sample.txt',
                  :body   => File.open("sample.txt")
                  )

バケット下の全オブジェクト一覧

client.list_objects(:bucket => bucket_name).contents.each do |object|
  puts object.key
end
#=> hoge/object1
#=> hoge/object2
#=> object3

バケット下のオブジェクト検索&一度に取得する数を設定

client.list_objects(:bucket => bucket_name, :prefix => 'hoge/', :max_keys => 2).contents.each do |object|
  puts object.key
end
#=> hoge/object1
#=> hoge/object2
  • :prefixで、検索条件指定(前方一致)
  • :max_keysで、取得数指定(デフォルトは1000)

オブジェクト取得

client.get_object(:bucket => 'bucket_name', :key => 'sample.txt').body.read

オブジェクト削除

client.delete_object(:bucket => 'bucket_name', :key => 'sample.txt')

Aws::S3::Resource(Object)を使う場合

resource作成

require 'aws-sdk'
bucket = Aws::S3::Resource.new(
                             :region => 'region',
                             :access_key_id => 'xxxxxx',
                             :secret_access_key => 'xxxxxx',
                             ).bucket('bucket_name')

アップロード

bucket.object('sample.txt').put(:body => File.open('sample.txt'))

マルチパートアップロード

bucket.object('sample.txt').upload_file('sample.txt')

オブジェクト存在チェック

bucket.object('sample.txt').exists?
#=> true or false

バケット下の全オブジェクト一覧

bucket.objects.each do |object|
  puts object.key
end
#=> hoge/object1
#=> hoge/object2
#=> object3

バケット下のオブジェクト検索&一度に取得する数を設定

bucket.objects(:prefix => 'hoge/', :max_keys => 2).each do |object|
  puts object.key
end
#=> hoge/object1
#=> hoge/object2
  • :prefixで、検索条件指定(前方一致)
  • :max_keysで、取得数指定(デフォルトは1000)

オブジェクト取得

bucket.object('sample.txt').get.body.read

オブジェクト削除

bucket.object('sample.txt').delete

署名付きURL

一定期間だけアクセスできるURLを作成する

require 'aws-sdk'
object = Aws::S3::Resource.new(
                               :region => 'region',
                               :access_key_id => 'xxxxxx',
                               :secret_access_key => 'xxxxxx',
                               ).bucket('bucket_name').object('sample.txt')

puts url = URI.parse(object.presigned_url(:get, expires_in: 30))
#=> https://bucket_name.s3-ap-northeast-1.amazonaws.com/sample.txt?X-Amz-Algorithm=AWS4-HM...
  • :expires_inで、有効期間(sec)を指定。デフォルト900sec。MAX 1week(604800sec)
174
154
1

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
174
154

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?