Client.Storage (Voile v0.1.23)

Copy Markdown View Source

Main storage interface that delegates to configured storage adapter.

Configuration

Set the storage adapter in your config:

config :voile, :storage_adapter, Client.Storage.S3

Or use environment variable:

export VOILE_STORAGE_ADAPTER="s3"  # or "local"

The adapter is automatically selected at runtime:

  • If VOILE_S3_ACCESS_KEY_ID and VOILE_S3_SECRET_ACCESS_KEY are set, uses S3
  • Otherwise, uses Local filesystem storage

Adapters

Summary

Functions

Delete a file using the configured storage adapter.

Generate a presigned URL for the given file key using the configured adapter.

Upload a file using the configured storage adapter.

Functions

delete(file_url, opts \\ [])

Delete a file using the configured storage adapter.

Options

  • :adapter - Override the default storage adapter
  • :delete_attachment - If true, also delete the attachment record from database (default: false)

Examples

{:ok, url} = Client.Storage.delete(url)
{:ok, url} = Client.Storage.delete(url, adapter: Client.Storage.S3)
{:ok, url} = Client.Storage.delete(url, delete_attachment: true)

presign(file_key, opts \\ [])

Generate a presigned URL for the given file key using the configured adapter.

For S3 adapters this should return a short-lived URL that allows direct GET access to the object. For local adapters it may return {:error, :not_supported}.

upload(file_params, opts \\ [])

Upload a file using the configured storage adapter.

Options

  • :adapter - Override the default storage adapter
  • :folder - Specify upload folder (e.g., "thumbnails", "attachments")
  • :unit_id - Unit ID for organizing files by unit
  • :generate_filename - Whether to generate a unique filename (default: true)
  • :preserve_extension - Whether to preserve original file extension (default: true)
  • :create_attachment - Whether to create an attachment record in the database (default: false)
  • :attachable_id - ID of the entity the attachment belongs to (required if create_attachment: true)
  • :attachable_type - Type of entity the attachment belongs to (required if create_attachment: true)
  • :access_level - Access level for the attachment (default: "restricted")
  • :file_type - Type of file (default: inferred from mime_type)

Examples

# Upload with default settings
{:ok, url} = Client.Storage.upload(upload)

# Upload to specific folder
{:ok, url} = Client.Storage.upload(upload, folder: "thumbnails")

# Upload with attachment creation
{:ok, url} = Client.Storage.upload(upload,
  create_attachment: true,
  attachable_id: user_id,
  attachable_type: "User",
  access_level: "restricted"
)