Skip to content

Instantly share code, notes, and snippets.

@leroyrosales
Created August 18, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leroyrosales/46fe4af5115a9e5e2f0fd78dcb91c2ae to your computer and use it in GitHub Desktop.
Save leroyrosales/46fe4af5115a9e5e2f0fd78dcb91c2ae to your computer and use it in GitHub Desktop.
Two ways to fetch a file from a url using Ruby
#!/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