stein v0.4.1 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
file()
View Source
file() :: Stein.Storage.FileUpload.t()
file() :: Stein.Storage.FileUpload.t()
A processed file ready for uploading
key()
View Source
key() :: String.t()
key() :: String.t()
Key for where the document will be stored
Must start with a leading /
local_path()
View Source
local_path() :: Path.t()
local_path() :: Path.t()
A local file path
opts()
View Source
opts() :: Keyword.t()
opts() :: Keyword.t()
Options for a function
url()
View Source
url() :: String.t()
url() :: String.t()
The URL for viewing the remote file
Link to this section Functions
delete(key)
View Source
delete(key()) :: :ok
delete(key()) :: :ok
Delete files from remote storage
download(key)
View Source
download(key()) :: {:ok, local_path()}
download(key()) :: {:ok, local_path()}
Download files from remote storage
Note: this creates a temporary file and must be cleaned up manually
prep_file(upload) View Source
Prepare a file for upload to the backend
Must be a Stein.Storage.FileUpload
, Plug.Upload
, or a map that
has the :path
key.
upload(file, key, opts) View Source
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"])
url(key, opts \\ []) View Source
Get the remote url for viewing an uploaded file
Link to this section Callbacks
delete(key)
View Source
delete(key()) :: :ok
delete(key()) :: :ok
Delete files from remote storage
download(key)
View Source
download(key()) :: {:ok, local_path()}
download(key()) :: {:ok, local_path()}
Download files from remote storage
Note: this creates a temporary file and must be cleaned up manually
upload(file, key, opts) View Source
Upload files to the remote storage
url(key, opts) View Source
Get the remote url for viewing an uploaded file