Apparently in Elixir the most direct (in the “dependencies-free” sense) way of downloading remote file is to use
https://www.erlang.org/doc/man/httpc.html#request-5
At least I haven’t found anything else so far. That seems OK as the task at hand is nothing complex. Just GET
a remote file over http(s) and save it to a local one:
:httpc.request(:get, {'https://my.file.server.net/filename', []}, [timeout: 5000], [stream: 'local_file'])
seems to do the job, but:
-
file is “binary” (in the general, not Elixir meaning) yet I see no difference between
body_format: :binary
and the default (string). Is there any? -
more importantly I can’t fathom how to make the downloaded file overwrite existing one at the local destination path. New file gets appended to the existing one instead of overwriting it. Is there any Erlang voodoo I am missing here or do I have to handle this myself?
-
and the most important question is: is there a way to limit the response size. I mean it’s great that I can limit the time of the request (
timeout
) so that this doesn’t hang there forever but what if for some reason I get the output of/dev/zero
for example instead of what I am expecting. IOW I don’t would like to store files only up to certain length (byte count). Any clues?