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
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")
Specs
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: noneobjects
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
anddelete
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: noneobjects
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
anddelete
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()
Lists all the files within a bucket.
Notes
- Policy permissions required:
buckets
permissions: noneobjects
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()]
Moves an existing file, optionally renaming it at the same time.
Notes
- Policy permissions required:
buckets
permissions: noneobjects
permissions:update
andselect
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: noneobjects
permissions:delete
andselect
Example
Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.remove(["public/avatar1", "private/avatar2"])
Replaces an existing file at the specified path with a new one.
Notes
- Policy permissions required:
buckets
permissions: noneobjects
permissions:update
andselect
Example
Supabase.storage()
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.update("public/avatar1.png", "/my/avatar/file.png")
Options
HTTP headers, for example :cache_control
Specs
Uploads a file to an existing bucket.
Notes
- Policy permissions required
buckets
permissions: noneobjects
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