googleapis / elixir-google-api

Elixir client libraries for accessing Google APIs.
https://hex.pm/users/google-cloud
Apache License 2.0
1.01k stars 459 forks source link

Google Cloud Storage stream download #2884

Open victorolinasc opened 4 years ago

victorolinasc commented 4 years ago

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?

sikula commented 2 years ago

Has the feature been added?

tjstankus commented 2 years ago

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: