LoginSignup
8
6

More than 5 years have passed since last update.

Rubyの'aws-sdk'の使い方が変わってた。

Posted at

RubyからS3へファイルをアップロードする

文献を漁るとAWS::S3.newとか書かれている記事が多いですが、本家のサイトを見るとどうやら違う模様。

はまったのでメモ。

gem

これは変わらず。

Gemfile
gem 'aws-sdk'

Gemfile.lockはこんな感じ。

Gemfile.lock
    aws-sdk (2.1.24)
      aws-sdk-resources (= 2.1.24)
    aws-sdk-core (2.1.24)
      jmespath (~> 1.0)
    aws-sdk-resources (2.1.24)
      aws-sdk-core (= 2.1.24)

使い方

こんなイメージ

require 'aws-sdk'

s3 = Aws::S3::Resource.new(access_key_id: "ACCESS_KEY_ID",
                      secret_access_key:  "SECRET_ACCESS_KEY",
                      region: "REGION")
obj = s3.bucket("BUCKET_NAME").object("PATH")

# string data
obj.put(body: 'Hello World!')

# IO object
File.open('source', 'rb') do |file|
  obj.put(body: file)
end

AWSAwsのアッパーキャメルケースになってた・・・。

8
6
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
8
6