elixir-google-api icon indicating copy to clipboard operation
elixir-google-api copied to clipboard

Google Cloud Storage stream download

Open victorolinasc opened this issue 6 years ago • 2 comments

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?

victorolinasc avatar Nov 25 '19 15:11 victorolinasc

Has the feature been added?

sikula avatar Feb 23 '22 17:02 sikula

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 Tesla client using an adapter with streaming capabilities and set the body_as: :stream option. Mint is one adapter that does streaming. There are other supported libraries for streaming in Tesla.
  • pass alt: "media" option to storage_objects_get. Without a streaming adapter you will get the entire file in the response body. With a streaming adapter, you get a stream.

tjstankus avatar May 23 '22 17:05 tjstankus