azure-oss/storage-blob-flysystem-bundle
Installation
composer require azure-oss/storage-blob-flysystem-bundle
Add to config/bundles.php:
return [
// ...
Azure\Storage\Blob\FlysystemBundle\AzureStorageBlobFlysystemBundle::class => ['all' => true],
];
Configuration
Define Azure Blob Storage credentials in .env:
AZURE_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net"
AZURE_STORAGE_CONTAINER="my-container"
First Use Case Inject the adapter in a service:
use Azure\Storage\Blob\FlysystemBundle\AzureStorageBlobFlysystemAdapter;
class MyService {
public function __construct(
private AzureStorageBlobFlysystemAdapter $adapter
) {}
public function uploadFile(string $path, string $content) {
$this->adapter->write($path, $content);
}
}
File Operations
// Upload
$adapter->write('folder/file.txt', 'content');
// Download
$content = $adapter->read('folder/file.txt');
// Delete
$adapter->delete('folder/file.txt');
Directory Handling
// List files
$files = $adapter->listContents('folder/', false);
// Create directory (implicit via write)
$adapter->write('folder/subfolder/file.txt', 'content');
Metadata & Visibility
// Set metadata
$adapter->setMetadata('file.txt', ['custom' => 'value']);
// Set visibility (private/public)
$adapter->setVisibility('file.txt', 'public');
Symfony Filesystem
Use with league/flysystem-bundle for seamless integration:
# config/packages/league_flysystem.yaml
flysystem:
storages:
azure:
adapter: azure_storage_blob
Event Dispatching
Listen to league.flysystem.* events for post-upload actions:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use League\Flysystem\Event\FileUploaded;
class UploadSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [FileUploaded::class => 'onUpload'];
}
public function onUpload(FileUploaded $event) {
// Process uploaded file
}
}
Connection String Security
AZURE_STORAGE_CONNECTION_STRING in source. Use .env or Symfony’s ParameterBag.Storage Blob Data Contributor).Path Handling
folder/File.txt ≠ folder/file.txt).$path = rtrim($path, '/');
Large File Uploads
config/packages/azure_storage_blob_flysystem.yaml:
azure_storage_blob:
options:
chunk_size: 16777216 # 16MB
Enable Logging
Add to config/packages/monolog.yaml:
handlers:
azure:
type: stream
path: "%kernel.logs_dir%/azure.log"
level: debug
channels: ["azure"]
Then enable the channel in config/packages/azure_storage_blob_flysystem.yaml:
azure_storage_blob:
options:
logger: true
Common Errors
| Error | Solution |
|---|---|
InvalidConnectionString |
Verify .env and connection string format. |
ContainerNotFound |
Ensure the container exists in Azure Portal or create it programmatically. |
AccessDenied |
Check Azure Storage account keys or SAS tokens. |
Custom Metadata Extend the adapter to add custom metadata handlers:
$adapter->extend('setCustomMetadata', function ($path, array $metadata) {
$this->client->getBlobProperties($this->container, $path);
// Modify metadata via Azure SDK
});
Event-Driven Workflows Use Symfony’s event system to trigger actions on file operations:
// config/packages/framework.yaml
framework:
event_dispatcher:
listeners:
'League\Flysystem\Event\FileUploaded': [App\Listener\AzureUploadListener]
Fallback Storage Implement a fallback adapter for offline scenarios:
use League\Flysystem\Filesystem;
use League\Flysystem\Storage\StorageInterface;
class FallbackAdapter implements StorageInterface {
public function write($path, $contents, Config $config) {
// Fallback logic (e.g., local filesystem)
}
// ... other methods
}
How can I help you explore Laravel packages today?