Ruby download large file from url






















My Ruby's a little rusty, so I'm sure this can be improved upon. First of all, there's no error handling. Also, maybe it could be separated into an object, away from the downloading itself, so that you'd just call autosleep. I could solve the problem by using:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 11 years, 9 months ago. Active 6 months ago. Viewed k times. I am on the Windows platform and I would prefer not to run any external program.

Improve this question. Radek Radek My solution is strongly based on snippets. Dejw: I did research and found an answered question here. Basically with the same code you gave me. The resp. I also found that rio. Moreover with my question nobody can say that such question was not answered yet :- — Radek.

The body part is exactly whole file. Response is created from headers http and body the file , so when You saves the body You saved the file ;- — Dawid. Is there going to be anything saved? Can I do resume of the file? Unfortunately not, because http.

To download a file in chunks and save it simultaneously see my edited answer below ;- Resuming is not easy, maybe You count bytes You saved and then skip them when You redownload the file file. Sample applications that cover common use cases in a variety of languages. Download, test drive, and tweak them yourself. Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri.

In the directory in which you opened IRB you will now find the image you downloaded. Problems with open-uri The thing is, using open-uri like this is not ideal. Notably, open-uri : leaves you vulnerable to remote code execution sometimes returns a Tempfile other times StringIO doesn't let you limit redirects or file size To solve all of this, Janko created the Down gem.

Improving downloads with the Down gem Let's download the same image using Down. Start by installing the gem:. Advanced usage Down takes a bunch of other options too, to control the download experience. Next steps In this post we've seen how to download images using both open-uri and Down. Rate this post. Sign up and start building Not ready yet?

Talk to an expert. Thanks for rating this post! The best way is IO. Show 3 more comments. Abram Clemens Helm Clemens Helm 3, 1 1 gold badge 19 19 silver badges 13 13 bronze badges. And more efficient as the output is not stored as a string in memory. Though it's actually useful having both answers displayed, so people like me can see both, think about the differences and then choose this answer consciously for the right reasons : — Maxwell Evans.

I'm getting a security warning from Rubocop when using open. I replaced it with URI. Read more about that cop here: rubydoc. Paperclip will take it from there. For future searchers, Paperclip no longer allows this by default: medium. I think this is the clearest way: require 'open-uri' File. Sage Ross Sage Ross 5 5 silver badges 12 12 bronze badges. It depends, but typically you'd do something like this inside a model or controller method.

In a Rails context, you're likely better off using the framework's tools like ActiveStorage, rather than writing directly to disk. This answer needs more visibility as Kernel open enables not only file access but also process invocation by prefixing a pipe symbol e. So, it may lead to a serious security risk by using variable input to the argument of Kernel open. Here's possibly the simplest way: IO. Down is much safer as it saves us from infinite redirect loops and remote code injection, and it is more user friendly as it always returns a Tempfile and lets us easily restrict file size.

Have you used alternative methods for downloading images in Ruby? Let me know how in the comments below or on Twitter at philnash. Originally published at www. The Cosmos HackAtom is here! Get feral when you answer to the greatest interview in history Share your philosophy. How to download image files in Ruby using the standard library open-uri. Open-uri extends Kernel open so that it can open URIs as if they were files.

We can download an image and then save it as a file using the Down gem. To download the image we can download the file to a Tempfile and then move it to a permanent location on our hard drive.



0コメント

  • 1000 / 1000