elixir-google-api
elixir-google-api copied to clipboard
Google Cloud Storage stream download
It would be nice if we could stream file contents in a nice to have Elixir API like File.Stream like:
https://www.poeticoding.com/elixir-streams-to-process-large-http-responses-on-the-fly/
Is that supported by the HTTP API?
Has the feature been added?
AFAICT streaming is not directly supported. However, I was able to get a stream in the response body with this code:
# returns {:ok, %Tesla.Env.t()}
# env.body contains a stream resource
def get_object_stream(token, bucket_name, object) do
{:ok, env} =
Tesla.client(
[
{Tesla.Middleware.Headers, [{"authorization", "Bearer #{token}"}]}
],
{Tesla.Adapter.Mint, [body_as: :stream]}
)
|> GoogleApi.Storage.V1.Api.Objects.storage_objects_get(bucket_name, object, alt: "media")
end
The two key bits of this code snippet:
- establish a
Teslaclient using an adapter with streaming capabilities and set thebody_as: :streamoption.Mintis one adapter that does streaming. There are other supported libraries for streaming inTesla. - pass
alt: "media"option tostorage_objects_get. Without a streaming adapter you will get the entire file in the response body. With a streaming adapter, you get a stream.