azure-oss/storage-blob-flysystem
Install the Package
composer require azure-oss/storage-blob-flysystem
For Laravel, prefer azure-oss/storage-blob-laravel for seamless integration.
Basic Usage
use AzureOss\Storage\Blob\BlobServiceClient;
use League\Flysystem\Filesystem;
use League\Flysystem\AzureBlob\AzureBlobAdapter;
// Initialize BlobServiceClient
$blobServiceClient = BlobServiceClient::createFromConnectionString(
env('AZURE_STORAGE_CONNECTION_STRING')
);
// Create Flysystem adapter
$adapter = new AzureBlobAdapter(
$blobServiceClient->getContainerClient('my-container')
);
// Instantiate Filesystem
$filesystem = new Filesystem($adapter);
First Use Case: Upload a File
$filesystem->write('path/to/file.txt', 'Hello Azure!');
storage-blob-laravel for pre-configured filesystem drivers.azure-oss/storage-blob for BlobServiceClient methods.Filesystem Operations Leverage Flysystem’s familiar API for:
$filesystem->write('file.txt', file_get_contents('local.txt'));
$content = $filesystem->read('file.txt');
$filesystem->createDirectory('folder');
$filesystem->deleteDirectory('folder');
Laravel Filesystem Integration
Add to config/filesystems.php:
'disks' => [
'azure' => [
'driver' => 'azure-blob',
'connection' => 'azure',
],
'azure-connection' => [
'driver' => 'azure-blob',
'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING'),
'container' => env('AZURE_CONTAINER_NAME'),
],
],
Use in code:
use Illuminate\Support\Facades\Storage;
Storage::disk('azure')->put('file.txt', 'Content');
Metadata and Custom Properties Use BlobServiceClient directly for advanced Azure features:
$blobClient = $blobServiceClient->getBlobClient('container', 'file.txt');
$blobClient->setMetadata(['customKey' => 'customValue']);
Event Handling Listen for Azure Storage events (e.g., blob uploads) via Azure Event Grid or SDK callbacks:
$blobServiceClient->getContainerClient('container')
->addEventSubscription('event-grid-endpoint', ['eventTypes' => ['Microsoft.Storage.BlobCreated']]);
AzureStorageException:
try {
$filesystem->write('file.txt', 'Content');
} catch (AzureStorageException $e) {
Log::error('Azure upload failed: ' . $e->getMessage());
}
Connection Strings
.env or environment variables:
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...
Container Existence
AzureBlobAdapter throws exceptions if not.$blobServiceClient->createContainer('my-container');
Permission Scopes
Write).$sasToken = $blobServiceClient->generateSasToken(
'container',
['sp' => 'rw'] // Read + Write
);
Large File Handling
$blobClient->uploadFromStream('file.txt', fopen('local.txt', 'r'), [
'blockSize' => 4 * 1024 * 1024, // 4MB blocks
]);
Timeouts
$blobServiceClient = BlobServiceClient::createFromConnectionString(
env('AZURE_STORAGE_CONNECTION_STRING'),
['timeout' => 30] // 30 seconds
);
AzureOss\Storage\Common\AzureLogger::setLogLevel(\Monolog\Logger::DEBUG);
AzureStorageException or AzureRequestException in Laravel logs.Custom Adapters
Extend AzureBlobAdapter for custom logic (e.g., auto-prefixing paths):
class CustomAzureBlobAdapter extends AzureBlobAdapter {
public function writeStream($path, $resource, array $options) {
$path = 'custom-prefix/' . $path;
return parent::writeStream($path, $resource, $options);
}
}
Event Subscribers Subscribe to Flysystem events for pre/post-actions:
$filesystem->addListener('preWrite', function ($event) {
Log::debug('Pre-write event for: ' . $event->getPath());
});
Laravel Service Providers
Bind custom adapters in AppServiceProvider:
$this->app->bind('azure-blob-adapter', function () {
return new CustomAzureBlobAdapter(
BlobServiceClient::createFromConnectionString(env('AZURE_STORAGE_CONNECTION_STRING'))
->getContainerClient('container')
);
});
https://<account>.blob.core.windows.net. Override if using custom endpoints:
BlobServiceClient::createFromConnectionString(
env('AZURE_STORAGE_CONNECTION_STRING'),
['endpointSuffix' => 'core.windows.net'] // Explicit suffix
);
$normalizedPath = strtolower($path);
How can I help you explore Laravel packages today?