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
write, read, delete) that works with local disk, S3, Google Cloud, Azure, FTP โ just change which "device" it points at.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
// 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
// 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
public/ are directly accessible. Store user uploads outside the web root and serve them through a PHP controller that checks permissions.visibility: 'public'.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
| Method | Description |
|---|---|
| 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 |
| Adapter | Driver 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.