From 3455885fd59d4b84ca5a9ae0146b455c4c57f583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20Barr=C3=A8re?= Date: Thu, 14 Sep 2023 11:06:08 +0200 Subject: [PATCH] Exemple de script qui fait du streaming direct vers le disque (#3452) * Start initial prototype * Implement example of streaming * Format --- scripts/req_stream.exs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 scripts/req_stream.exs diff --git a/scripts/req_stream.exs b/scripts/req_stream.exs new file mode 100644 index 0000000000..df54c0dc86 --- /dev/null +++ b/scripts/req_stream.exs @@ -0,0 +1,22 @@ +Mix.install([ + {:req, "~> 0.4.0"} +]) + +large_file_url = "https://www.data.gouv.fr/fr/datasets/r/c83ba91e-2cd1-40f7-a632-eb0a76d83c49" +# small_file_url = "https://httpbin.org/range/100000" + +url = large_file_url +# url = small_file_url + +# Download file to disk via an IO.Stream +file = File.stream!("test.data", [:write]) + +try do + # NOTE: decode_body is apparently disabled on response streaming + # https://hexdocs.pm/req/Req.Steps.html#decode_body/1 + response = Req.get!(url, into: file) + # :body is just a ref to the File.Stream + IO.inspect(response, IEx.inspect_opts()) +after + :ok = File.close(file) +end