Supabase.Storage (Supabase v0.3.0) View Source

Module to work with Supabase storage and the same API the storage-js client provides.

Link to this section Summary

Functions

Creates a new Storage bucket

Create signed url to download file without requiring permissions. This URL can be valid for a set number of seconds.

Deletes an existing bucket. A bucket can't be deleted with existing objects inside it. You must first empty() the bucket.

Downloads a file.

Removes all objects inside a single bucket.

Retrieves the details of an existing Storage bucket.

Lists all the files within a bucket.

Retrieves the details of all Storage buckets within an existing product.

Moves an existing file, optionally renaming it at the same time.

Deletes files within the same bucket

Replaces an existing file at the specified path with a new one.

Uploads a file to an existing bucket.

Link to this section Functions

Specs

create_bucket(Supabase.Connection.t(), binary()) ::
  {:error, map()} | {:ok, map()}

Creates a new Storage bucket

Notes

  • Policy permissions required:
    • buckets permissions: insert
    • objects permissions: none

Example

Supabase.storage(session.access_token)
|> Supabase.Storage.create_bucket("avatars")
Link to this function

create_bucket!(conn, id)

View Source

Specs

create_bucket!(Supabase.Connection.t(), binary()) :: map()
Link to this function

create_signed_url(conn, path, expires_in)

View Source

Create signed url to download file without requiring permissions. This URL can be valid for a set number of seconds.

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: select

Example

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.create_signed_url("public/avatar1", 60)

Specs

delete_bucket(Supabase.Connection.t(), binary() | Supabase.Storage.Bucket.t()) ::
  {:error, map()} | {:ok, map()}

Deletes an existing bucket. A bucket can't be deleted with existing objects inside it. You must first empty() the bucket.

Notes

  • Policy permissions required:
    • buckets permsisions: select and delete
    • objects permissions: none

Example

Supabase.storage()
|> Supabase.Storage.delete_bucket("avatars")

Specs

download(Supabase.Connection.t(), binary() | Supabase.Storage.Object.t()) ::
  {:error, map()} | {:ok, binary()}

Downloads a file.

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: select

Examples

{:ok, blob} =
  Supabase.storage()
  |> Supabase.Storage.from("avatars")
  |> Supabase.Storage.download("public/avatar2.png")

File.write("/tmp/avatar2.png", blob)

Specs

download!(Supabase.Connection.t(), binary() | Supabase.Storage.Object.t()) ::
  binary()

Specs

empty_bucket(Supabase.Connection.t(), binary() | Supabase.Storage.Bucket.t()) ::
  {:error, map()} | {:ok, map()}

Removes all objects inside a single bucket.

Notes

  • Policy permissions required
    • buckets permissions: select
    • objects permissions: select and delete

Example

Supabase.storage(session.access_token)
|> Supabase.Storage.empty_bucket("avatars")

Specs

get_bucket(Supabase.Connection.t(), binary()) ::
  {:error, map()} | {:ok, Supabase.Storage.Bucket.t()}

Retrieves the details of an existing Storage bucket.

Notes

  • Policy permissions required
    • buckets permissions: select
    • objects permissions: none

Example

{:ok, bucket} =
  Supabase.storage()
  |> Supabase.Storage.get_bucket("avatars")

Specs

get_bucket!(Supabase.Connection.t(), binary()) :: Supabase.Storage.Bucket.t()
Link to this function

list(conn, options \\ [])

View Source

Lists all the files within a bucket.

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: select

Example

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.list(path: "public")

Options

  • :path - The folder path

Specs

list_buckets(Supabase.Connection.t()) ::
  {:error, map()} | {:ok, [Supabase.Storage.Bucket.t()]}

Retrieves the details of all Storage buckets within an existing product.

Notes

  • Policy permissions required
    • buckets permissions: select
    • objects permissions: none

Example

{:ok, buckets} =
  Supabase.storage(session.access_token)
  |> Supabase.Storage.list_buckets()

Specs

list_buckets!(Supabase.Connection.t()) :: [Supabase.Storage.Bucket.t()]
Link to this function

move(conn, from_path, to_path)

View Source

Moves an existing file, optionally renaming it at the same time.

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: update and select

Example

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.move("public/avatar1.png", "private/avatar2.png")

Deletes files within the same bucket

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: delete and select

Example

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.remove(["public/avatar1", "private/avatar2"])
Link to this function

update(conn, path, file, file_options \\ [])

View Source

Replaces an existing file at the specified path with a new one.

Notes

  • Policy permissions required:
    • buckets permissions: none
    • objects permissions: update and select

Example

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.update("public/avatar1.png", "/my/avatar/file.png")

Options

HTTP headers, for example :cache_control

Link to this function

upload(conn, path, file, file_options \\ [])

View Source

Specs

upload(Supabase.Connection.t(), binary(), binary(), keyword()) ::
  {:error, map()} | {:ok, map()}

Uploads a file to an existing bucket.

Notes

  • Policy permissions required
    • buckets permissions: none
    • objects permissions: insert

Example

Basic

Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.upload("public/avatar1.png", "/local/path/to/avatar1.png")

Phoenix Live Upload

def handle_event("save", _params, socket) do
  uploaded_files =
    consume_uploaded_entries(socket, :avatar, fn %{path: path}, entry ->
      {:ok, %{"Key" => blob_key}} =
        Supabase.storage(socket.assigns.access_token)
        |> Supabase.Storage.from("avatars")
        |> Supabase.Storage.upload(
          "public/" <> entry.client_name, path, content_type: entry.client_type)

      blob_key
    )

  {:noreply, assign(socket, uploaded_files: uploaded_files)}
end