PhoenixKit.Modules.Storage.Provider behaviour (phoenix_kit v1.7.42)

Copy Markdown View Source

Behavior for storage providers.

Each storage provider (local, S3, B2, R2) must implement this behavior to provide file storage and retrieval capabilities.

Summary

Callbacks

Deletes a file from the storage provider.

Checks if a file exists in the storage provider.

Gets a public URL for a file in the storage provider.

Retrieves a file from the storage provider.

Stores a file in the storage provider.

Tests the connection to the storage provider.

Callbacks

delete_file(bucket, file_path)

@callback delete_file(
  bucket :: PhoenixKit.Modules.Storage.Bucket.t(),
  file_path :: String.t()
) ::
  :ok | {:error, term()}

Deletes a file from the storage provider.

Parameters

  • bucket - The bucket configuration
  • file_path - Path to the file in storage

Returns

  • :ok - File deleted successfully
  • {:error, reason} - Failed to delete file

file_exists?(bucket, file_path)

@callback file_exists?(
  bucket :: PhoenixKit.Modules.Storage.Bucket.t(),
  file_path :: String.t()
) ::
  boolean()

Checks if a file exists in the storage provider.

Parameters

  • bucket - The bucket configuration
  • file_path - Path to the file in storage

Returns

  • true if file exists, false otherwise

public_url(bucket, file_path)

@callback public_url(
  bucket :: PhoenixKit.Modules.Storage.Bucket.t(),
  file_path :: String.t()
) ::
  String.t() | nil

Gets a public URL for a file in the storage provider.

Parameters

  • bucket - The bucket configuration
  • file_path - Path to the file in storage

Returns

  • Public URL string or nil if not applicable

retrieve_file(bucket, file_path, destination_path)

@callback retrieve_file(
  bucket :: PhoenixKit.Modules.Storage.Bucket.t(),
  file_path :: String.t(),
  destination_path :: String.t()
) :: :ok | {:error, term()}

Retrieves a file from the storage provider.

Parameters

  • bucket - The bucket configuration
  • file_path - Path to the file in storage
  • destination_path - Local path where to save the file

Returns

  • {:ok, file_path} - File retrieved successfully
  • {:error, reason} - Failed to retrieve file

store_file(bucket, source_path, destination_path, opts)

@callback store_file(
  bucket :: PhoenixKit.Modules.Storage.Bucket.t(),
  source_path :: String.t(),
  destination_path :: String.t(),
  opts :: keyword()
) :: {:ok, String.t()} | {:error, term()}

Stores a file in the storage provider.

Parameters

  • bucket - The bucket configuration
  • source_path - Path to the source file on local filesystem
  • destination_path - Relative path where to store the file
  • opts - Additional options

Returns

  • {:ok, url} - File stored successfully, returns public URL
  • {:error, reason} - Failed to store file

test_connection(bucket)

@callback test_connection(bucket :: PhoenixKit.Modules.Storage.Bucket.t()) ::
  :ok | {:error, term()}

Tests the connection to the storage provider.

Parameters

  • bucket - The bucket configuration

Returns

  • :ok - Connection successful
  • {:error, reason} - Connection failed