Storage.DirectUpload (PhoenixContribStorage v0.1.0)
View SourceHandles 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
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.
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)
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
Verifies that a direct upload was successful by checking if the file exists.