Help us understand the problem. What is going on with this article?

AWS SDK for Ruby V2でS3をいろいろ使う

More than 1 year has passed since last update.

インストール

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)
jrits
信頼と魅力のある先進のITをもとに、お客様のワークスタイル・イノベーションの実現を目指します。
http://www.jrits.co.jp/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした