Google Cloud Storage client for Elixir, built on the JSON API over Finch.
Provides bucket and object management, simple and resumable uploads, and
streamed downloads. Responses are decoded JSON resources (maps with string
keys); errors are GcpGcs.Error.t/0 structs.
Configuration
Production (default)
No configuration is required — requests go to storage.googleapis.com.
Authentication uses, in order: a configured Goth instance, then the
gcloud CLI. See GcpGcs.Auth.
# Optional: use Goth
config :gcp_gcs, :goth, MyApp.GothDevelopment / test (emulator)
config :gcp_gcs, :emulator,
scheme: "http",
host: "localhost",
port: 4443See GcpGcs.Config for the full set of options.
Error handling
All functions return {:ok, result}/:ok or {:error, %GcpGcs.Error{}}.
Pattern match on the error code:
case GcpGcs.get_object("my-bucket", "missing.txt") do
{:ok, object} -> object
{:error, %GcpGcs.Error{code: :not_found}} -> :missing
{:error, %GcpGcs.Error{} = err} -> raise "GCS error: #{err}"
endExamples
# Buckets
{:ok, _bucket} = GcpGcs.create_bucket("my-project", "my-bucket", location: "US")
{:ok, %{items: buckets}} = GcpGcs.list_buckets("my-project")
# Upload + download
{:ok, _object} = GcpGcs.put_object("my-bucket", "hello.txt", "Hello!")
{:ok, "Hello!"} = GcpGcs.download("my-bucket", "hello.txt")
# Stream a large file in and out with constant memory
{:ok, _} = GcpGcs.upload_file("my-bucket", "big.bin", "/tmp/big.bin")
:ok = GcpGcs.download_to_file("my-bucket", "big.bin", "/tmp/copy.bin")
# List a "directory"
{:ok, %{items: items, prefixes: dirs}} =
GcpGcs.list_objects("my-bucket", prefix: "logs/", delimiter: "/")
Summary
Functions
Clears the cached access token, forcing a refresh. See GcpGcs.Auth.clear_cache/0.
Concatenates source objects into a new object. See GcpGcs.Object.compose/4.
Copies an object. See GcpGcs.Object.copy/5.
Creates a bucket. See GcpGcs.Bucket.create/3.
Deletes a bucket. See GcpGcs.Bucket.delete/2.
Deletes an object. See GcpGcs.Object.delete/3.
Downloads an object into memory. See GcpGcs.Download.download/3.
Folds an object's body through a reducer. See GcpGcs.Download.stream/5.
Streams an object to a local file. See GcpGcs.Download.download_to_file/4.
Gets a bucket's metadata. See GcpGcs.Bucket.get/2.
Gets an object's metadata. See GcpGcs.Object.get/3.
Lists buckets in a project. See GcpGcs.Bucket.list/2.
Lists objects in a bucket. See GcpGcs.Object.list/2.
Moves/renames an object (HNS buckets). See GcpGcs.Object.move/4.
Uploads in-memory data in one request. See GcpGcs.Upload.put_object/4.
Rewrites/copies an object, following rewrite tokens. See GcpGcs.Object.rewrite/5.
Starts a resumable upload session. See GcpGcs.Upload.start_resumable_upload/3.
Patches a bucket's metadata. See GcpGcs.Bucket.update/3.
Patches an object's metadata. See GcpGcs.Object.update/4.
Uploads from a binary, {:file, path}, or stream. See GcpGcs.Upload.upload/4.
Uploads one chunk to a resumable session. See GcpGcs.Upload.upload_chunk/3.
Resumable streamed upload of a local file. See GcpGcs.Upload.upload_file/4.
Resumable upload from an enumerable of binaries. See GcpGcs.Upload.upload_stream/4.
Functions
Clears the cached access token, forcing a refresh. See GcpGcs.Auth.clear_cache/0.
Concatenates source objects into a new object. See GcpGcs.Object.compose/4.
Copies an object. See GcpGcs.Object.copy/5.
Creates a bucket. See GcpGcs.Bucket.create/3.
Deletes a bucket. See GcpGcs.Bucket.delete/2.
Deletes an object. See GcpGcs.Object.delete/3.
Downloads an object into memory. See GcpGcs.Download.download/3.
Folds an object's body through a reducer. See GcpGcs.Download.stream/5.
Streams an object to a local file. See GcpGcs.Download.download_to_file/4.
Gets a bucket's metadata. See GcpGcs.Bucket.get/2.
Gets an object's metadata. See GcpGcs.Object.get/3.
Lists buckets in a project. See GcpGcs.Bucket.list/2.
Lists objects in a bucket. See GcpGcs.Object.list/2.
Moves/renames an object (HNS buckets). See GcpGcs.Object.move/4.
Uploads in-memory data in one request. See GcpGcs.Upload.put_object/4.
Rewrites/copies an object, following rewrite tokens. See GcpGcs.Object.rewrite/5.
Starts a resumable upload session. See GcpGcs.Upload.start_resumable_upload/3.
Patches a bucket's metadata. See GcpGcs.Bucket.update/3.
Patches an object's metadata. See GcpGcs.Object.update/4.
Uploads from a binary, {:file, path}, or stream. See GcpGcs.Upload.upload/4.
Uploads one chunk to a resumable session. See GcpGcs.Upload.upload_chunk/3.
Resumable streamed upload of a local file. See GcpGcs.Upload.upload_file/4.
Resumable upload from an enumerable of binaries. See GcpGcs.Upload.upload_stream/4.