Installation Add the package via Composer:
composer require azurespring/bincamp-bundle
Register the bundle in config/app.php under providers:
AzureSpring\BincampBundle\BincampBundle::class,
Configuration Publish the default config:
php artisan vendor:publish --provider="AzureSpring\BincampBundle\BincampServiceProvider"
Update config/bincamp.php with your Azure Blob Storage credentials (connection string, container name, etc.).
First Use Case: Uploading a File
Inject the BincampService into a controller or service:
use AzureSpring\BincampBundle\Service\BincampService;
public function uploadFile(Request $request, BincampService $bincamp)
{
$file = $request->file('file');
$path = $bincamp->upload($file, 'user-uploads'); // 'user-uploads' is the blob container
return response()->json(['path' => $path]);
}
First Use Case: Retrieving a File
public function downloadFile(BincampService $bincamp)
{
$blobName = 'user-uploads/example.pdf';
$stream = $bincamp->download($blobName);
return response()->stream(function () use ($stream) {
fpassthru($stream);
}, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="example.pdf"',
]);
}
File Uploads
$path = $bincamp->upload($file, $container, ['metadata' => ['user_id' => 1]]);
$uploadId = $bincamp->createBlockBlob($container, $blobName);
$bincamp->uploadBlock($uploadId, $chunk, $index);
$bincamp->commitBlockBlob($uploadId);
File Retrieval
$stream = $bincamp->download($blobName);
$sasUrl = $bincamp->generateSasUrl($blobName, '+r', 3600); // Valid for 1 hour
File Management
$blobs = $bincamp->listBlobs($container, 'prefix-');
$bincamp->deleteBlob($blobName);
Metadata and Properties
$bincamp->setMetadata($blobName, ['custom_key' => 'value']);
$metadata = $bincamp->getMetadata($blobName);
Laravel Filesystem Integration:
Use the BincampAdapter to integrate with Laravel's filesystem:
$filesystem = Storage::disk('azure');
$filesystem->put('remote-file.txt', 'contents');
Configure in config/filesystems.php:
'azure' => [
'driver' => 'azure',
'container' => 'my-container',
],
Event Handling: Bind to blob events (e.g., upload completion) using Laravel's event system:
event(new \AzureSpring\BincampBundle\Events\BlobUploaded($blobName, $metadata));
Validation: Validate file types/sizes before upload:
$request->validate([
'file' => 'required|file|mimes:pdf,docx|max:10240', // 10MB
]);
Connection Issues
config/bincamp.php is correct and has the right permissions.$bincamp->testConnection();
StorageException often indicates invalid credentials or network issues.Blob Naming Collisions
$blobName = 'uploads/' . Str::uuid() . '.' . $file->getClientOriginalExtension();
Large File Timeouts
max_execution_time or use queue jobs for large uploads:
UploadLargeFileJob::dispatch($file, $container)->onQueue('uploads');
SAS Token Expiry
generateSasUrl():
$sasUrl = $bincamp->generateSasUrl($blobName, '+r', 86400); // 24 hours
Case Sensitivity
$blobName = strtolower($blobName);
Enable Logging:
Add to config/bincamp.php:
'debug' => env('BINCAMP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Check Blob Existence:
if (!$bincamp->blobExists($blobName)) {
// Handle missing blob
}
Retry Failed Operations:
Use Laravel's retry helper for transient failures:
retry(5, function () use ($bincamp, $blobName) {
$bincamp->download($blobName);
});
Custom Storage Accounts Override the default connection in config:
'connections' => [
'primary' => [
'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING'),
'container' => env('AZURE_CONTAINER'),
],
'backup' => [
'connection_string' => env('AZURE_BACKUP_CONNECTION_STRING'),
'container' => env('AZURE_BACKUP_CONTAINER'),
],
],
Switch dynamically:
$bincamp->setConnection('backup');
Middleware for File Access Create middleware to validate SAS tokens or user permissions:
public function handle($request, Closure $next)
{
$blobName = $request->route('blob');
if (!$bincamp->validateSasToken($blobName, $request->query('token'))) {
abort(403);
}
return $next($request);
}
Custom Metadata Handlers
Extend the BincampService to auto-populate metadata:
$bincamp->extend(function ($service) {
$service->beforeUpload(function ($file, $container, $options) {
$options['metadata']['uploaded_by'] = auth()->id();
return $options;
});
});
Fallback Storage Implement a fallback to local storage if Azure is unavailable:
try {
$path = $bincamp->upload($file, $container);
} catch (\Exception $e) {
$path = Storage::disk('local')->putFile('fallback', $file);
}
How can I help you explore Laravel packages today?