Skip to main content

src/aws@s3@streaming.erl

-module(aws@s3@streaming).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aws/s3/streaming.gleam").
-export([download_to_bit_array_max/3]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Hand-written convenience wrappers around streaming S3 operations.\n"
    "\n"
    " The base `get_object_streaming(client, input) -> Result(streaming.Response,\n"
    " runtime.ClientError)` is emitted by the codegen directly on the\n"
    " `s3` module (see `s3.get_object_streaming`). This module adds\n"
    " pure convenience helpers on top — patterns that come up often\n"
    " enough that surfacing them with their own typed error is\n"
    " worthwhile.\n"
).

-file("src/aws/s3/streaming.gleam", 23).
?DOC(
    " Convenience: stream a GetObject response and materialise its\n"
    " body as a `BitArray`, refusing if cumulative size would exceed\n"
    " `max_bytes`. Thin wrapper over `streaming.collect_to_bit_array_max`\n"
    " pinning the input error type to `runtime.ClientError`.\n"
    "\n"
    " Typical \"download a smallish-bounded object\" case: small JSON /\n"
    " config blobs / log shards where the wire bytes fit in memory\n"
    " but the caller wants a hard ceiling. For multi-GB objects skip\n"
    " this helper and consume chunks via `streaming.fold_chunks`.\n"
).
-spec download_to_bit_array_max(
    aws@services@s3:client(),
    aws@services@s3:get_object_request(),
    integer()
) -> {ok, bitstring()} |
    {error,
        aws@streaming:collect_error(aws@internal@client@runtime:client_error())}.
download_to_bit_array_max(Client, Input, Max_bytes) ->
    aws@streaming:collect_to_bit_array_max(
        aws@services@s3:get_object_streaming(Client, Input),
        Max_bytes
    ).