LoginSignup
7
5

More than 5 years have passed since last update.

[Ruby] aws-sdk v3.0.1 で S3 にファイルアップロード

Posted at

本題

aws-sdk を使ってS3にファイルアップロードする。
version 3.0 を使っている日本語記事が見つからなかったので書く。

install

Gemfileに以下を追加
gem 'aws-sdk'
bundle install
$ bundle install

How To Use

require 'aws-sdk'
class Caws
  def initialize
    @bucket_name = 'bucketname'

    Aws.config.update({
        region: 'ap-northeast-1',
        credentials: Aws::Credentials.new('ACCESS-KEY', 'SECRET-KEY')
    })

    s3 = Aws::S3::Resource.new
    @bucket = s3.bucket(@bucket_name)
  end

  def send(s3_path,upd_path,file_name)
    o = @bucket.object(s3_path + '/' + file_name)
    o.upload_file(upd_path + '/' + file_name)
  end
end

ACCESS-KEY とか SECRET-KEY はここで取得できた。
https://console.aws.amazon.com/iam/home?#/security_credential

How to use send

  • s3_path : s3 の指定したbucket上のpath
  • upd_path : ローカルにあるファイルパス
  • file_name : ファイルネーム

Test

class CawsTest < Test::Unit::TestCase

  def setup
    @aws = Caws.new
  end

  def test_upd
    @aws.send("s3/file/path","/local/file/path", "file.txt")
  end
end

Reference

7
5
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
7
5