View Source FileStore protocol (file_store v0.4.0)

FileStore allows you to read, write, upload, download, and interact with files, regardless of where they are stored.

Adapters

This package ships with the following adapters:

The documentation for each adapter includes an example that demonstrates it's usage.

Link to this section Summary

Functions

Copy a file to a new location.

Delete a file from the store.

Delete files in bulk.

Download a file from the store and save it to the given path.

Get URL for your file, assuming that the file is publicly accessible.

Generate a signed URL for your file. Any user with this URL should be able to access the file.

List files in the store.

Read the contents of a file in store into memory.

Renames a file from one name to another.

Retrieve information about a file from the store.

Upload a file to the store. If a file with the given key already exists, it will be overwritten.

Write a file to the store. If a file with the given key already exists, it will be overwritten.

Link to this section Types

Specs

delete_all_opts() :: [{:prefix, binary()}]

Specs

key() :: binary()

Specs

list_opts() :: [{:prefix, binary()}]

Specs

public_url_opts() :: [content_type: binary(), disposition: binary()]

Specs

signed_url_opts() :: [
  content_type: binary(),
  disposition: binary(),
  expires_in: integer()
]

Specs

t() :: term()

Specs

write_opts() :: [content_type: binary(), disposition: binary()]

Link to this section Functions

Specs

copy(t(), key(), key()) :: :ok | {:error, term()}

Copy a file to a new location.

Examples

iex> FileStore.copy(store, "path/foo.txt", "path/bar.txt")
:ok

Specs

delete(t(), key()) :: :ok | {:error, term()}

Delete a file from the store.

Examples

iex> FileStore.delete(store, "foo") :ok

Link to this function

delete_all(store, opts \\ [])

View Source

Specs

delete_all(t(), delete_all_opts()) :: :ok | {:error, term()}

Delete files in bulk.

Options

  • :prefix - Only delete keys matching the given prefix.

Examples

iex> FileStore.delete_all(store) :ok

iex> FileStore.delete_all(store, prefix: "foo/") :ok

Link to this function

download(store, key, destination)

View Source

Specs

download(t(), key(), Path.t()) :: :ok | {:error, term()}

Download a file from the store and save it to the given path.

Examples

iex> FileStore.download(store, "foo", "/path/to/bar.txt")
:ok
Link to this function

get_public_url(store, key, opts \\ [])

View Source

Specs

get_public_url(t(), key(), public_url_opts()) :: binary()

Get URL for your file, assuming that the file is publicly accessible.

Options

  • :content_type - Force the Content-Type of the response.
  • :disposition - Force the Content-Disposition of the response.

Examples

iex> FileStore.get_public_url(store, "foo")
"https://mybucket.s3-us-east-1.amazonaws.com/foo"
Link to this function

get_signed_url(store, key, opts \\ [])

View Source

Specs

get_signed_url(t(), key(), signed_url_opts()) ::
  {:ok, binary()} | {:error, term()}

Generate a signed URL for your file. Any user with this URL should be able to access the file.

Options

  • :expires_in - The number of seconds before the URL expires.
  • :content_type - Force the Content-Type of the response.
  • :disposition - Force the Content-Disposition of the response.

Examples

iex> FileStore.get_signed_url(store, "foo")
{:ok, "https://s3.amazonaws.com/mybucket/foo?X-AMZ-Expires=3600&..."}
Link to this function

list!(store, opts \\ [])

View Source

Specs

list!(t(), list_opts()) :: Enumerable.t()

List files in the store.

Options

  • :prefix - Only return keys matching the given prefix.

Examples

iex> Enum.to_list(FileStore.list!(store))
["bar", "foo/bar"]

iex> Enum.to_list(FileStore.list!(store, prefix: "foo"))
["foo/bar"]

Specs

read(t(), key()) :: {:ok, binary()} | {:error, term()}

Read the contents of a file in store into memory.

Examples

iex> FileStore.read(store, "foo")
{:ok, "hello world"}
Link to this function

rename(store, src, dest)

View Source

Specs

rename(t(), key(), key()) :: :ok | {:error, term()}

Renames a file from one name to another.

Note: Some underlying adapters can not do this in an atomic fashion.

Examples

iex> FileStore.rename(store, "path/foo.txt", "path/bar.txt")
:ok

Specs

stat(t(), key()) :: {:ok, FileStore.Stat.t()} | {:error, term()}

Retrieve information about a file from the store.

Examples

iex> FileStore.stat(store, "foo")
{:ok, %FileStore.Stat{key: "foo", etag: "2e5pd429", size: 24}}
Link to this function

upload(store, source, key)

View Source

Specs

upload(t(), Path.t(), key()) :: :ok | {:error, term()}

Upload a file to the store. If a file with the given key already exists, it will be overwritten.

Examples

iex> FileStore.upload(store, "/path/to/bar.txt", "foo")
:ok
Link to this function

write(store, key, content, opts \\ [])

View Source

Specs

write(t(), key(), binary(), write_opts()) :: :ok | {:error, term()}

Write a file to the store. If a file with the given key already exists, it will be overwritten.

Options

* `:content_type` - Sets the content type hint for the adapter.
* `:disposition` - Sets the content disposition hint for the adapter.

Examples

iex> FileStore.write(store, "foo", "hello world")
:ok