Storage.DirectUpload (PhoenixContribStorage v0.1.0)

View Source

Handles direct uploads to storage services.

Direct uploads allow clients to upload files directly to the storage service (like S3) without going through your Phoenix server, improving performance and reducing server load.

Usage

# Generate a signed URL for direct upload
{:ok, upload_data} = Storage.DirectUpload.signed_url(
  filename: "document.pdf",
  content_type: "application/pdf",
  byte_size: 1024000
)

# The client uploads directly using the returned data
# Then creates the blob record:
{:ok, blob} = Storage.DirectUpload.create_blob_after_direct_upload(upload_data)

Summary

Functions

Creates a blob record after a successful direct upload.

Creates a complete direct upload flow with verification.

Generates a signed URL and metadata for direct upload.

Verifies that a direct upload was successful by checking if the file exists.

Functions

create_blob_after_direct_upload(upload_data)

Creates a blob record after a successful direct upload.

This should be called after the client has successfully uploaded the file using the signed URL data.

finalize_direct_upload(key, service_name \\ nil)

prepare_direct_upload(opts)

Creates a complete direct upload flow with verification.

Example

{:ok, upload_data} = Storage.DirectUpload.prepare_direct_upload(
  filename: "image.jpg",
  content_type: "image/jpeg",
  byte_size: 512000
)

# Client uploads file...

{:ok, blob} = Storage.DirectUpload.finalize_direct_upload(upload_data.key)

signed_url(opts \\ [])

Generates a signed URL and metadata for direct upload.

Options

  • :filename - Original filename (required)
  • :content_type - MIME type (inferred from filename if not provided)
  • :byte_size - File size in bytes (required for some services)
  • :service_name - Storage service to use (defaults to configured default)
  • :expires_in - URL expiration time in seconds (default: 3600)
  • :max_file_size - Maximum allowed file size (default: 100MB)
  • :metadata - Additional metadata to store with the blob

Returns

Returns {:ok, upload_data} where upload_data contains:

  • :url - The upload URL
  • :fields - Form fields required for the upload
  • :key - The storage key that will be used
  • :blob_attributes - Attributes to create the blob after upload

verify_upload(key, service_name \\ nil)

Verifies that a direct upload was successful by checking if the file exists.