Integrate Laravel with Azure Blob Storage for file upload, listing, downloading, and deleting

To integrate Laravel with Azure Blob Storage for file upload, listing, downloading, and deleting, you can use the Microsoft Azure Storage Blob SDK for PHP or configure Laravel to use Azure Blob Storage as a filesystem driver.


Steps to Use Azure Blob Storage in Laravel:

1. Install Required Packages

First, install the Azure Blob Storage PHP SDK and Flysystem Azure adapter:

composer require microsoft/azure-storage-blob league/flysystem-azure-blob

2. Configure Azure Blob Storage in Laravel

Add Azure Blob Storage configuration to your Laravel project.

  • In config/filesystems.php, add a new disk for Azure:
'disks' => [
    // Other disks...

    'azure' => [
        'driver'    => 'azure',
        'endpoint'  => env('AZURE_ENDPOINT', 'https://<account_name>.blob.core.windows.net'),
        'name'      => env('AZURE_STORAGE_ACCOUNT'),
        'key'       => env('AZURE_STORAGE_KEY'),
        'container' => env('AZURE_STORAGE_CONTAINER'),
    ],
],
  • In your .env file, set the Azure credentials:
AZURE_ENDPOINT=https://<account_name>.blob.core.windows.net
AZURE_STORAGE_ACCOUNT=<your_account_name>
AZURE_STORAGE_KEY=<your_account_key>
AZURE_STORAGE_CONTAINER=<your_container_name>

3. File Operations

You can now use Laravel's Storage facade to perform operations on Azure Blob Storage.

Upload File
use Illuminate\Support\Facades\Storage;

public function uploadFile(Request $request)
{
    $file = $request->file('file');
    $path = Storage::disk('azure')->put('folder-name', $file);

    return response()->json(['path' => $path], 200);
}
List Files
public function listFiles()
{
    $files = Storage::disk('azure')->files('folder-name');

    return response()->json(['files' => $files], 200);
}
Download File
public function downloadFile($filename)
{
    $content = Storage::disk('azure')->get('folder-name/' . $filename);
    $headers = ['Content-Type' => 'application/octet-stream'];

    return response($content, 200, $headers);
}
Delete File
public function deleteFile($filename)
{
    Storage::disk('azure')->delete('folder-name/' . $filename);

    return response()->json(['message' => 'File deleted successfully'], 200);
}

4. Error Handling

Always wrap file operations in try-catch blocks to handle exceptions gracefully, especially for network-related issues.

use Illuminate\Support\Facades\Log;

try {
    // Perform file operation
} catch (\Exception $e) {
    Log::error('Azure Blob Storage Error: ' . $e->getMessage());
    return response()->json(['error' => 'Operation failed'], 500);
}

5. Testing

  • Upload files using a form with enctype="multipart/form-data".
  • Use endpoints to list, download, and delete files.
  • Check Azure Portal to confirm the file operations.

This setup allows seamless integration between Laravel and Azure Blob Storage.

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example