Skip to content

Instantly share code, notes, and snippets.

@mejiaro
Last active February 15, 2016 23:10
Show Gist options
  • Save mejiaro/dee1633b7a65035e262a to your computer and use it in GitHub Desktop.
Save mejiaro/dee1633b7a65035e262a to your computer and use it in GitHub Desktop.
Ruby script to download files from AWS S3, when you have the AWS key.
require 'net/http'
require 'net/ftp'
require 'uri'
def http_download_uri(uri, filename)
puts "Starting HTTP download for: " + uri.to_s
http_object = Net::HTTP.new("my_AWS_S3_bucket_url", 80)
#http_object.use_ssl = true if uri.scheme == 'https'
puts uri
begin
http_object.start do |http|
request = Net::HTTP::Get.new uri
http.read_timeout = 500
http.request request do |response|
open File.join("/", "Users","my_username","my_images", "my_image_name"), 'wb' do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
rescue Exception => e
puts "=> Exception: '#{e}'. Skipping download."
return
end
puts "Download success!"
end
#based on https://gist.github.com/johnjohndoe/4301582
#the uri is the full aws url.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment