Behaviour for storage adapters.
PhxMediaLibrary ships with the following adapters:
PhxMediaLibrary.Storage.Disk- Local filesystem (default)PhxMediaLibrary.Storage.S3- Amazon S3 / compatible servicesPhxMediaLibrary.Storage.Memory- In-memory storage (for testing)
Implementing a Custom Adapter
defmodule MyApp.Storage.CustomAdapter do
@behaviour PhxMediaLibrary.Storage
@impl true
def put(path, content, opts) do
# Store content at path
:ok
end
# ... implement other callbacks
endThen configure it:
config :phx_media_library,
disks: [
custom: [
adapter: MyApp.Storage.CustomAdapter,
# adapter-specific options
]
]
Summary
Callbacks
Delete the file at the given path.
Check if a file exists at the given path.
Retrieve content from the given path.
Get the full filesystem path (local storage only).
Generate a presigned URL for direct client-to-storage uploads.
Store content at the given path.
Get a URL for the file.
Types
@type content() :: binary() | {:stream, Enumerable.t()}
@type opts() :: keyword()
@type path() :: String.t()
@type presigned_opts() :: [ expires_in: pos_integer(), content_type: String.t(), content_length_range: {non_neg_integer(), pos_integer()} ]
@type url_opts() :: [ signed: boolean(), expires_in: pos_integer(), content_type: String.t(), disposition: String.t() ]
Callbacks
Delete the file at the given path.
Check if a file exists at the given path.
Retrieve content from the given path.
Get the full filesystem path (local storage only).
@callback presigned_upload_url(path(), presigned_opts(), opts()) :: {:ok, String.t(), map()} | {:error, term()}
Generate a presigned URL for direct client-to-storage uploads.
Returns {:ok, url, fields} where url is the upload endpoint and
fields is a map of form fields (for POST-based uploads) or an empty
map (for PUT-based uploads).
Only applicable to remote storage backends (e.g. S3). Local storage adapters do not need to implement this callback.
Options
:expires_in— URL expiration in seconds (default: adapter-specific):content_type— expected content type of the upload:content_length_range—{min, max}byte range tuple for upload size validation
Store content at the given path.
Get a URL for the file.