Install the Bundle
composer require oneup/flysystem-bundle
Add to config/bundles.php:
return [
// ...
Oneup\FlysystemBundle\OneupFlysystemBundle::class => ['all' => true],
];
Configure a Filesystem
Edit config/packages/oneup_flysystem.yaml:
oneup_flysystem:
adapters:
my_adapter:
local:
directory: '%kernel.project_dir%/var/files'
filesystems:
my_filesystem:
adapter: my_adapter
alias: my_filesystem
First Use Case: Upload a File
Inject the Filesystem service and use it:
use League\Flysystem\FilesystemInterface;
public function uploadFile(FilesystemInterface $filesystem, UploadedFile $file)
{
$filesystem->writeStream('path/to/file.txt', $file->openFile());
}
Adapter Configuration
oneup_flysystem.yaml (supports all Flysystem adapters).adapters:
s3_adapter:
aws_s3:
bucket: 'my-bucket'
region: 'us-east-1'
options:
credentials:
key: '%env(AWS_KEY)%'
secret: '%env(AWS_SECRET)%'
Filesystem Usage
$filesystem->write('file.txt', 'Hello, Flysystem!');
$contents = $filesystem->read('file.txt');
$filesystem->writeStream('large_file.zip', fopen('local/path.zip', 'r'));
Symfony Integration
FilesystemInterface to templates:
<img src="{{ asset(filesystem.url('image.jpg')) }}">
Oneup\FlysystemBundle\Doctrine\Types\FileType for file metadata storage.Dynamic Filesystems
$filesystem = $container->get('oneup_flysystem.my_filesystem');
Custom Adapters
Extend League\Flysystem\AdapterInterface and register via config:
adapters:
custom_adapter:
custom:
class: App\Adapter\CustomAdapter
config: { ... }
Event Listeners
Listen to filesystem events (e.g., oneup_flysystem.pre_write):
use Oneup\FlysystemBundle\Event\FilesystemEvent;
public function onPreWrite(FilesystemEvent $event) {
$event->setPath(strtolower($event->getPath()));
}
Caching
Enable caching for adapters (e.g., cache: true in local adapter config).
Adapter-Specific Errors
AsyncAwsS3 or AwsS3 is installed (composer require async-aws/s3 or aws/aws-sdk-php).phpseclib/phpseclib is installed and credentials are correct.directory path is writable (chmod -R 777 var/files).Configuration Overrides
%kernel.project_dir% or environment variables (%env(FILES_DIR)%).Flysystem V2/V3 Migration
league/flysystem-aws-s3-v3 for V3).Log Adapter Errors Enable debug mode and check Symfony logs for adapter-specific errors (e.g., connection timeouts).
Validate Config
Use the oneup_flysystem:validate command to check configuration:
php bin/console oneup_flysystem:validate
Test Locally First
Start with a local adapter before switching to cloud services (e.g., S3/Azure).
Custom Metadata
Use FilesystemInterface::getMetadata() and extend with adapter-specific methods.
Symfony Messenger Dispatch filesystem events as messages for async processing:
$this->messageBus->dispatch(new FilesystemProcessedEvent($path));
Dependency Injection Bind custom adapters as services:
services:
app.custom_adapter:
class: App\Adapter\CustomAdapter
arguments:
- '%env(CUSTOM_CONFIG)%'
How can I help you explore Laravel packages today?