Created
May 1, 2014 22:32
-
-
Save wbingli/6f8ed65dded41fb9ccb2 to your computer and use it in GitHub Desktop.
Download big file with ruby and basic authentication
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 'net/http' | |
require 'uri' | |
def downloadFile(filePath, url,username, password) | |
uri = URI(URI.encode(url)) | |
Net::HTTP.start(uri.host,uri.port) do |http| | |
request = Net::HTTP::Get.new uri.path | |
request.basic_auth username,password | |
http.request request do |response| | |
open filePath, 'w' do |io| | |
response.read_body do |chunk| | |
puts "Writing #{chunk.length} bits ..." | |
io.write chunk | |
end | |
end | |
end | |
end | |
end | |
urlPath = "http://<link_to_big_file>" | |
downloadFile '/tmp/test.zip',urlPath,"username","password" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment