azure-oss/storage-blob-symfony
Install the Package
composer require azure-oss/storage-blob-symfony
Ensure league/flysystem-bundle is also installed (required dependency).
Configure Flysystem
Add Azure Blob Storage configuration to config/packages/flysystem.yaml:
flysystem:
storages:
azure_blob:
adapter: azure_oss
options:
service: 'azure://<connection>'
bucket: 'your-container-name'
endpoint: 'https://<account>.blob.core.windows.net'
credentials:
key: '%env(AZURE_STORAGE_KEY)%'
secret: '%env(AZURE_STORAGE_SECRET)%'
First Use Case: Upload a File
Inject the Flysystem service and use it like any other Flysystem adapter:
use League\Flysystem\FilesystemInterface;
public function uploadFile(FilesystemInterface $storage)
{
$storage->write('path/to/file.txt', 'file contents');
}
Service Configuration
Reuse the same connection across multiple storages by defining a service key in flysystem.yaml:
flysystem:
storages:
primary_blob:
adapter: azure_oss
service: 'azure://default'
backup_blob:
adapter: azure_oss
service: 'azure://default'
Environment-Specific Config
Use Symfony’s %env% for dynamic values (e.g., endpoint, credentials):
options:
endpoint: '%env(AZURE_BLOB_ENDPOINT)%'
Integration with Laravel
If using Laravel, alias the Symfony service in config/app.php:
'filesystems' => [
'disks' => [
'azure' => [
'driver' => 'flysystem',
'adapter' => 'azure_oss',
'options' => [
'service' => 'azure://default',
'bucket' => env('AZURE_CONTAINER'),
],
],
],
],
Streaming Large Files Leverage Flysystem’s streaming capabilities for large uploads/downloads:
$storage->writeStream('large-file.zip', fopen('local/path.zip', 'r'));
Metadata Handling Set custom metadata during upload:
$storage->write(
'file.txt',
'contents',
[
'custom-meta' => 'value',
'content-type' => 'text/plain',
]
);
Connection String Format
The service key expects a URI-like string (e.g., azure://default). Ensure it matches the configured connection in azure-oss/storage-blob-flysystem.
Credentials Handling
Avoid hardcoding secrets. Use Symfony’s %env% or Laravel’s .env:
credentials:
key: '%env(AZURE_STORAGE_KEY)%'
secret: '%env(AZURE_STORAGE_SECRET)%'
Endpoint Overrides
If using Azure’s public endpoint (core.windows.net), omit the endpoint key. For private endpoints, specify the full URL.
CORS and Public Access
Ensure the Azure Storage container has the correct CORS rules or public access level (blob or container). Private containers require signed URLs for direct access.
Flysystem Bundle Version
Ensure compatibility with league/flysystem-bundle (v2.x+). Check the package’s composer.json for constraints.
endpoint, credentials, and bucket (container) names. Use Azure Storage Explorer to validate container existence.Storage Blob Data Contributor or equivalent permissions.monolog:
handlers:
main:
level: debug
Custom Adapters
Extend the underlying azure-oss/storage-blob-flysystem adapter for custom logic (e.g., pre/post-upload hooks).
Event Listeners Use Symfony’s event system to intercept file operations:
// src/EventListener/AzureBlobListener.php
public function onFileUploaded(FilesystemEvent $event) {
if ($event->getStorage()->getAdapter() instanceof AzureOssAdapter) {
// Custom logic
}
}
Flysystem Plugins
Integrate plugins like league/flysystem-aws-s3-v4 for shared functionality (e.g., caching, checksums).
Testing
Mock the FilesystemInterface in tests:
$storage = $this->createMock(FilesystemInterface::class);
$storage->method('write')->willReturn(true);
How can I help you explore Laravel packages today?