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

AWS SDK for Ruby(V2)を利用して、Amazon S3を操作する

More than 5 years have passed since last update.

しばらく触らないうちにV2とか出てたりしたので、基本操作のおさらい。

Gemでインストール

$ gem install aws-sdk-core
$ gem list aws-sdk-core
*** LOCAL GEMS ***
aws-sdk-core (2.0.3)

Credentialsの設定

~/.aws/credentials
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key

バケットの一覧表示

aws-sdk-ruby-s3.rb
require "aws-sdk-core"

Aws.config[:region] = 'ap-northeast-1'
s3 = Aws::S3::Client.new

resp = s3.list_buckets
puts resp.buckets.map(&:name)

バケットの作成

s3.create_bucket(bucket: "BucketName")

バケットの削除

s3.delete_bucket(bucket: "BucketName")

オブジェクトの一覧表示

resp = s3.list_objects(bucket: "BucketName")
resp.contents.each do |object|
    puts "#{object.key}"
end

オブジェクトの削除

s3.delete_object(
    bucket: "BucketName",
    key: "sample.jpg"
)

ファイルのアップロード

file_open = File.open("sample.jpg")
file_name = File.basename("sample.jpg")

s3.put_object(
    bucket: "BucketName",
    body: file_open,
    key: file_name
)

ファイルのダウンロード

File.open("sample.jpg", "w") do |file|
    s3.get_object(bucket: "BucketName", key: "sample.jpg") do |chunk|
        file.write(chunk)
    end
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
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
ユーザーは見つかりませんでした