Skip to content

Commit

Permalink
Exemple de script qui fait du streaming direct vers le disque (#3452)
Browse files Browse the repository at this point in the history
* Start initial prototype

* Implement example of streaming

* Format
  • Loading branch information
thbar authored Sep 14, 2023
1 parent e25f9c7 commit 3455885
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scripts/req_stream.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3455885

Please sign in to comment.