Created
August 18, 2019 12:24
-
-
Save leroyrosales/46fe4af5115a9e5e2f0fd78dcb91c2ae to your computer and use it in GitHub Desktop.
Two ways to fetch a file from a url using 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
#!/usr/bin/env ruby | |
# usage: saveimages.rb <url> | |
# locally save a file | |
## uri option - problematic https://twin.github.io/improving-open-uri/ | |
# require "open-uri" | |
# open("https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/test.original.jpg") do |image| | |
# File.open("./test.jpg", "wb") do |file| | |
# file.write(image.read) | |
# end | |
# end | |
## down option - preferred | |
require "down" | |
require "fileutils" | |
tempfile = Down.download( | |
"https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/test.original.jpg", | |
max_redirects: 5 | |
) | |
FileUtils.mv(tempfile.path, "./nina.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment