Install the Package
composer require anglemx/file-storage-bundle
Configure the Bundle
Add the bundle to config/bundles.php:
return [
// ...
Angle\FileStorageBundle\AngleFileStorageBundle::class => ['all' => true],
];
Define Storage Backend
Configure in config/packages/angle_file_storage.yaml (or .env):
angle_file_storage:
type: local
container: "uploads"
For AWS S3 or Azure Blob Storage, replace type and add required credentials.
First Use Case: Uploading a File
Inject the FileStorage service and use it:
use Angle\FileStorageBundle\Service\FileStorage;
public function uploadFile(FileStorage $storage)
{
$filePath = $storage->upload('path/to/local/file.txt', 'destination/file.txt');
return $filePath;
}
File Operations
$storage->upload($localPath, $remotePath)$storage->download($remotePath, $localPath)$storage->delete($remotePath)$storage->exists($remotePath)Streaming Large Files
Use uploadStream() and downloadStream() for memory efficiency:
$storage->uploadStream($stream, 'large-file.zip');
Dynamic Backend Switching
Override the type in config or via environment variables for different environments (e.g., local for dev, aws_s3 for prod).
Integration with Symfony Components
FileStorageEvents (e.g., FileUploadedEvent).FileStorage to a custom interface for testing:
services:
App\Service\CustomStorage:
arguments:
$storage: '@angle_file_storage.file_storage'
Presigned URLs (AWS S3) Generate temporary URLs for secure downloads:
$url = $storage->getPresignedUrl('file.txt', '+1 hour');
StorageException).FileStorage in unit tests using PHPUnit’s createMock():
$mockStorage = $this->createMock(FileStorage::class);
$mockStorage->method('upload')->willReturn('mocked/path');
Azure Blob Storage Validation
azure:list will fail if the service is misconfigured. Validate credentials first:
php bin/console angle:file-storage:validate
Local Path Resolution
uploads) are resolved relative to var/. Use absolute paths for clarity:
container: "/var/www/uploads" # Absolute path
AWS SDK Dependencies
aws/aws-sdk-php is installed and configured. Conflicts may arise with other AWS SDK versions.Permission Issues
var/ directory. Set:
chmod -R 775 var/
Environment Variables
.env and validate with:
php bin/console debug:container angle_file_storage
aws_region, username, and secret in the AWS console.az storage blob list (Azure CLI).Custom Backends
Extend Angle\FileStorageBundle\Service\StorageInterface to add support for Google Cloud Storage or FTP.
Event Subscribers
Listen for FileUploadedEvent to trigger notifications or metadata updates:
use Angle\FileStorageBundle\Event\FileUploadedEvent;
public static function getSubscribedEvents()
{
return [
FileUploadedEvent::NAME => 'onFileUploaded',
];
}
Configuration Overrides Dynamically switch backends per request using middleware:
$storage->setType('aws_s3'); // Override for specific requests
Symfony Cache Integration Cache presigned URLs or metadata to reduce backend calls:
$this->cache->get('presigned_url_' . $filePath, function() use ($storage) {
return $storage->getPresignedUrl($filePath);
});
aws_region or secret can be null for local storage but are required for cloud backends.How can I help you explore Laravel packages today?