Created
July 8, 2011 17:50
-
-
Save nathancolgate/1072361 to your computer and use it in GitHub Desktop.
Copying a file from HTTP to S3 via a local temp file in Ruby
This file contains 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
# This copies a file from HTTP to S3 | |
require 'net/http' | |
require 'aws/s3' | |
http_url = 'http://www.google.com/intl/en_com/images/srpr/logo1w.png' | |
http_domain = http_url.gsub('http://','').split('/')[0] | |
http_path = '/' + http_url.gsub('http://','').split('/')[1..999].join('/') | |
file_name = File.basename(http_url) | |
s3_path = "path/to/new/#{file_name}" | |
# Storing the file locally in the tmp folder | |
tmpfile_path = File.join('.',File.basename(file_name)) | |
Net::HTTP.start(http_domain) { |http| | |
resp = http.get(http_path) | |
File.open(tmpfile_path, "w") { |file| | |
file.write(resp.body) | |
} | |
} | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => 'your_access_key_id', | |
:secret_access_key => 'your_secret_access_key' | |
) | |
AWS::S3::S3Object.store(s3_path, open(tmpfile_path), 'your_bucket_name') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment