stein_storage v0.1.0 Stein.Storage behaviour View Source

Stein.Storage covers uploading, downloading, and deleting remote files

Available backends

FileBackend

The Stein.Storage.FileBackend is available for development purposes.

For the file backend, you can configure the folder Stein should use. This should be a local folder that Elixir has read/write permissions to. It should end with a trailing slash.

config :stein_storage,
  backend: :file,
  file_backend_folder: "uploads/"

The default folder is Stein's priv/files.

To fully support the file storage, you should also add a new Plug.Static to your endpoint. This will let the URLs the backend returns load.

 if Mix.env() == :dev do
   plug(Plug.Static, at: "/uploads", from: "uploads/files")
 end

S3Backend

The Stein.Storage.S3Backend handles uploading, downloading, and deletes from Amazon S3.

For the S3 backend, you can also configure the bucket Stein should upload to.

config :stein_storage,
  backend: :s3,
  bucket: "my-bucket"

MockBackend

The Stein.Storage.MockBackend mocks out all actions for use in tests. Each action is a no-op.

config :stein_storage,
  backend: :test

Link to this section Summary

Types

A processed file ready for uploading

Key for where the document will be stored

A local file path

Options for a function

The URL for viewing the remote file

Functions

Delete files from remote storage

Download files from remote storage

Prepare a file for upload to the backend

Upload files to the remote storage

Get the remote url for viewing an uploaded file

Callbacks

Delete files from remote storage

Download files from remote storage

Upload files to the remote storage

Get the remote url for viewing an uploaded file

Link to this section Types

A processed file ready for uploading

Key for where the document will be stored

Must start with a leading /

Link to this type

local_path()

View Source
local_path() :: Path.t()

A local file path

Options for a function

The URL for viewing the remote file

Link to this section Functions

Link to this function

delete(key)

View Source
delete(key()) :: :ok

Delete files from remote storage

Link to this function

download(key)

View Source
download(key()) :: {:ok, local_path()}

Download files from remote storage

Note: this creates a temporary file and must be cleaned up manually

Link to this function

prep_file(upload)

View Source
prep_file(file()) :: file()
prep_file(Plug.Upload.t()) :: file()
prep_file(%{path: String.t()}) :: file()

Prepare a file for upload to the backend

Must be a Stein.Storage.FileUpload, Plug.Upload, or a map that has the :path key.

Link to this function

upload(file, key, opts)

View Source
upload(file(), key(), opts()) ::
  :ok | {:error, :invalid_extension} | {:error, :uploading}

Upload files to the remote storage

Limiting extensions

You can limit extensions with the extensions option. Only the extensions in the list will be allowed, any other extension will be rejected with {:error, :invalid_extension}.

Each extension should start with a ..

Stien.Storage.upload(file, key, extensions: [".jpg", ".png"])
Link to this function

url(key, opts \\ [])

View Source
url(key(), opts()) :: url()

Get the remote url for viewing an uploaded file

Link to this section Callbacks

Link to this callback

delete(key)

View Source
delete(key()) :: :ok

Delete files from remote storage

Link to this callback

download(key)

View Source
download(key()) :: {:ok, local_path()}

Download files from remote storage

Note: this creates a temporary file and must be cleaned up manually

Link to this callback

upload(file, key, opts)

View Source
upload(file(), key(), opts()) :: :ok | :error

Upload files to the remote storage

Link to this callback

url(key, opts)

View Source
url(key(), opts()) :: url()

Get the remote url for viewing an uploaded file