Last active
December 10, 2015 16:29
-
-
Save ppworks/4461718 to your computer and use it in GitHub Desktop.
fog gemを使って、特定のURLのファイルをS3へ保存する ref: http://qiita.com/items/7f3c53a3198a1c7636ef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment