LoginSignup
26
28

More than 5 years have passed since last update.

fog gemを使って、特定のURLのファイルをS3へ保存する

Last updated at Posted at 2013-01-05
s3.rb
require 'rubygems'
require 'open-uri'

storage = Fog::Storage.new(
  provider: 'AWS',
  aws_access_key_id:ENV['AWS_S3_KEY_ID'],
  aws_secret_access_key:ENV['AWS_S3_SECRET_KEY'],
  region: ENV['AWS_REGION']
)
bucket = storage.directories.get(ENV['AWS_S3_BUCKET'])
bucket = storage.directories.create(key: ENV['AWS_S3_BUCKET']) unless bucket

path_to_save = 'users/images/hoge2.png'
image_url = 'http://a0.twimg.com/profile_images/2900491556/9d2bf873770958647f18b8e61af31f1a_normal.png'

file = bucket.files.create(key: path_to_save, public: true, body: open(image_url))
puts file.public_url

region list

  • eu-west-1 : 欧州 – アイルランド
  • sa-east-1 : 南米 – サンパウロ
  • us-east-1 : 米国 – バージニア北部
  • ap-northeast-1 : アジア – 東京
  • us-west-2 : 米国 – オレゴン
  • us-west-1 : 米国 – 北カリフォルニア
  • ap-sourtheast-1 : アジア – シンガポール
  • ap-sourtheast-2 : アジア – シドニー
26
28
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
26
28