PhoenixKit.Storage (phoenix_kit v1.6.4)
View SourceStorage context for managing files, buckets, and dimensions.
Provides a distributed file storage system with support for multiple storage providers (local filesystem, AWS S3, Backblaze B2, Cloudflare R2) with automatic redundancy and failover capabilities.
Features
- Multi-location storage with configurable redundancy (1-5 copies)
- Support for local, S3, B2, and R2 storage providers
- Automatic variant generation for images and videos
- Priority-based storage selection
- Built-in usage tracking and statistics
- PostgreSQL-backed file registry
Summary
Functions
Calculates free space for a bucket.
Calculates storage usage for a bucket in MB.
Returns an %Ecto.Changeset{} for tracking bucket changes.
Returns an %Ecto.Changeset{} for tracking dimension changes.
Returns an %Ecto.Changeset{} for tracking file changes.
Returns an %Ecto.Changeset{} for tracking file instance changes.
Creates a new bucket.
Creates a new dimension.
Creates a directory if it doesn't exist.
Creates a new file record.
Creates a new file instance.
Deletes a bucket.
Deletes a dimension.
Deletes a file.
Deletes file data from all storage buckets.
Deletes a file instance.
Checks if a file exists in storage.
Gets the absolute path for local storage.
Gets a single bucket by ID.
Gets a bucket by name.
Gets the current storage configuration.
Gets a single dimension by ID.
Gets a dimension by name.
Gets a single file by ID.
Gets a file by its hash.
Gets a single file instance by ID.
Gets the bucket IDs where a file instance is stored.
Gets a file instance by file ID and variant name.
Gets a public URL for a file.
Gets a public URL for a file by file ID.
Gets a public URL for a specific file variant by file ID.
Gets a public URL for a specific file variant.
Returns a list of all storage buckets, ordered by priority.
Returns a list of all dimensions, ordered by size (width x height).
Returns enabled dimensions for a specific file type.
Gets enabled buckets, ordered by priority.
Returns a list of file instances for a given file.
Returns a list of files, optionally filtered by bucket.
Resets all dimensions to default seeded values. Deletes all current dimensions and recreates the 8 default ones.
Retrieves a file from storage by file ID.
Retrieves a file by its hash.
Stores a file in the storage system.
Stores a file in buckets with hierarchical path structure.
Updates a bucket.
Updates the default storage path.
Updates a dimension.
Updates a file.
Updates a file instance.
Updates a file instance's processing status.
Updates a file instance with file information after processing.
Validates and normalizes a storage path.
Functions
Calculates free space for a bucket.
For local storage, checks actual disk space. For cloud storage, returns the configured max_size_mb minus usage.
Calculates storage usage for a bucket in MB.
Returns total size of all files stored in this bucket by summing up all file instances that have locations in this bucket.
Returns an %Ecto.Changeset{} for tracking bucket changes.
Returns an %Ecto.Changeset{} for tracking dimension changes.
Returns an %Ecto.Changeset{} for tracking file changes.
Returns an %Ecto.Changeset{} for tracking file instance changes.
Creates a new bucket.
Examples
iex> create_bucket(%{name: "Local Storage", provider: "local"})
{:ok, %Bucket{}}
iex> create_bucket(%{name: nil})
{:error, %Ecto.Changeset{}}
Creates a new dimension.
Examples
iex> create_dimension(%{name: "thumbnail", width: 150, height: 150})
{:ok, %Dimension{}}
iex> create_dimension(%{name: nil})
{:error, %Ecto.Changeset{}}
Creates a directory if it doesn't exist.
Creates a new file record.
This only creates the database record. Use store_file/4 to actually
store the file data in storage buckets.
Creates a new file instance.
Deletes a bucket.
Examples
iex> delete_bucket(bucket)
{:ok, %Bucket{}}
iex> delete_bucket(bucket)
{:error, %Ecto.Changeset{}}
Deletes a dimension.
Examples
iex> delete_dimension(dimension)
{:ok, %Dimension{}}
iex> delete_dimension(dimension)
{:error, %Ecto.Changeset{}}
Deletes a file.
This only removes the database record. Use delete_file_data/1 to
remove the actual file data from storage buckets.
Deletes file data from all storage buckets.
Deletes a file instance.
Checks if a file exists in storage.
Gets the absolute path for local storage.
Gets a single bucket by ID.
Returns nil if bucket does not exist.
Gets a bucket by name.
Gets the current storage configuration.
Gets a single dimension by ID.
Gets a dimension by name.
Gets a single file by ID.
Gets a file by its hash.
Gets a single file instance by ID.
Gets the bucket IDs where a file instance is stored.
Returns a list of bucket IDs from the file_locations for the given file instance.
Gets a file instance by file ID and variant name.
Gets a public URL for a file.
Gets a public URL for a file by file ID.
Convenience function that fetches the file and returns its URL.
Examples
iex> get_public_url_by_id("018e3c4a-9f6b-7890-abcd-ef1234567890")
"https://cdn.example.com/12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_original.jpg"
iex> get_public_url_by_id("invalid-id")
nil
Gets a public URL for a specific file variant by file ID.
Examples
iex> get_public_url_by_id("018e3c4a-9f6b-7890-abcd-ef1234567890", "thumbnail")
"https://cdn.example.com/12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_thumbnail.jpg"
Gets a public URL for a specific file variant.
Variants
For images: "original", "thumbnail", "small", "medium", "large" For videos: "original", "360p", "720p", "1080p", "video_thumbnail"
Examples
iex> get_public_url_by_variant(file, "thumbnail")
"https://cdn.example.com/12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_thumbnail.jpg"
iex> get_public_url_by_variant(file, "medium")
"https://cdn.example.com/12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_medium.jpg"
Returns a list of all storage buckets, ordered by priority.
Returns a list of all dimensions, ordered by size (width x height).
Returns enabled dimensions for a specific file type.
Gets enabled buckets, ordered by priority.
Returns a list of file instances for a given file.
Returns a list of files, optionally filtered by bucket.
Options
:bucket_id- Filter by bucket ID:limit- Maximum number of results:offset- Number of results to skip:order_by- Ordering (default:[desc: :inserted_at])
Resets all dimensions to default seeded values. Deletes all current dimensions and recreates the 8 default ones.
Retrieves a file from storage by file ID.
Will try buckets in priority order until the file is found.
Retrieves a file by its hash.
Stores a file in the storage system.
This will:
- Store the file in multiple buckets based on redundancy settings
- Generate variants if enabled
- Create database records for the file and its variants
Options
:filename- Original filename (required):content_type- MIME type (required):size_bytes- File size in bytes (required):user_id- User ID who owns the file:metadata- Additional metadata map
Stores a file in buckets with hierarchical path structure.
Path Structure
Files are stored using the pattern:
{user_id[0..1]}/{hash[0..1]}/{full_hash}/{full_hash}_{variant}.{format}
Examples
User ID: "12345678" File hash: "a1b2c3d4e5f6..." Original: "12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_original.jpg" Thumbnail: "12/a1/a1b2c3d4e5f6/a1b2c3d4e5f6_thumbnail.jpg"
Updates a bucket.
Examples
iex> update_bucket(bucket, %{name: "New Name"})
{:ok, %Bucket{}}
iex> update_bucket(bucket, %{name: nil})
{:error, %Ecto.Changeset{}}
Updates the default storage path.
Updates a dimension.
Examples
iex> update_dimension(dimension, %{name: "New Name"})
{:ok, %Dimension{}}
iex> update_dimension(dimension, %{name: nil})
{:error, %Ecto.Changeset{}}
Updates a file.
Updates a file instance.
Updates a file instance's processing status.
Updates a file instance with file information after processing.
Validates and normalizes a storage path.
Returns {:ok, relative_path} if valid, or error tuple if invalid.