Lesson 43 โ€ข Advanced

    File Storage Abstraction ๐Ÿ’พ

    Abstract file storage with Flysystem to seamlessly switch between local disk, Amazon S3, and cloud providers using one unified API.

    What You'll Learn in This Lesson

    • โ€ข Why file storage abstraction prevents vendor lock-in
    • โ€ข Use Flysystem's unified API: write, read, delete, copy, move
    • โ€ข Switch between local and S3 with one config change
    • โ€ข Generate signed URLs for temporary secure access
    • โ€ข Stream large files without exhausting memory

    Flysystem Unified API

    Flysystem provides a single API for all file operations. Write code once against the Filesystem interface, then swap adapters (local, S3, GCS) by changing configuration. This means your app works identically in development (local) and production (S3) without code changes.

    Try It: File Storage Operations

    Write, read, copy, move, delete, and list files with different adapters

    Try it Yourself ยป
    JavaScript
    // File Storage Abstraction with Flysystem
    console.log("=== Why Abstract File Storage? ===");
    console.log();
    console.log("  Without abstraction:");
    console.log("  โŒ file_put_contents() ties you to local disk");
    console.log("  โŒ Switching to S3 means rewriting every file operation");
    console.log("  โŒ Can't test without real filesystem");
    console.log();
    console.log("  With Flysystem:");
    console.log("  โœ… Same API for local, S3, GCS, FTP, SFTP, Azure");
    console.log("  โœ… Switch storage by changing on
    ...

    Signed URLs & Streaming

    Signed URLs provide temporary access to private files โ€” generate a URL that expires in 60 minutes for invoice downloads. For large files (backups, exports), use streaming to avoid loading the entire file into memory. A 2GB file streamed uses only ~8KB of RAM.

    Try It: Signed URLs & Multi-Disk

    Generate expiring download links and configure multi-disk storage

    Try it Yourself ยป
    JavaScript
    // Storage Patterns: Signed URLs, Streaming, Multi-disk
    console.log("=== Signed URLs (Temporary Access) ===");
    console.log();
    
    class SecureStorage {
      constructor(secretKey) { this.secretKey = secretKey; this.files = {}; }
    
      generateSignedUrl(path, expiresIn) {
        let expires = Date.now() + expiresIn * 1000;
        let signature = btoa(path + ":" + expires + ":" + this.secretKey).substring(0, 16);
        let url = "https://cdn.myapp.com/" + path + "?expires=" + expires + "&sig=" + signature;
    
        co
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Storing uploads in the web root โ€” Files in public/ are directly accessible. Store user uploads outside the web root and serve them through a PHP controller that checks permissions.
    โš ๏ธ
    Not setting visibility โ€” S3 objects default to private. If you need public access (avatars), explicitly set visibility: 'public'.
    ๐Ÿ’ก
    Pro Tip: Use league/flysystem-aws-s3-v3 for S3. In tests, use the InMemory adapter โ€” it's instant and requires no filesystem at all.

    ๐Ÿ“‹ Quick Reference โ€” File Storage

    MethodDescription
    write()Create or overwrite a file
    readStream()Read file as stream (memory efficient)
    temporaryUrl()Generate signed URL with expiration
    visibility()Set public or private access
    AdapterDriver for specific storage (Local, S3, etc.)

    ๐ŸŽ‰ Lesson Complete!

    You can now abstract file storage! Next, learn to optimize PHP performance and tune OPcache.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service