DEV Community

Cover image for How to connection between laravel and Azure Blob ?
Youssef Ahmed
Youssef Ahmed

Posted on

How to connection between laravel and Azure Blob ?

Integrating Azure Blob Storage with Laravel allows you to store files in Microsoft's scalable cloud storage instead of your local server. This approach improves reliability, scalability, and storage management for production applications.

In this guide, you'll learn how to connect Laravel 11, 12, and 13 to Azure Blob Storage and upload files with just a few steps.

Prerequisites

Before getting started, make sure you have:

  • A Laravel 11, 12, or 13 application.
  • An Azure account.
  • An Azure Storage Account with a Blob Container created.
  • Composer installed.

Step 1: Install the Azure Storage Package

Install the Laravel Azure Storage package:

composer require matthewbdaly/laravel-azure-storage
Enter fullscreen mode Exit fullscreen mode

Then, add a new disk inside config/filesystems.php:

'azure' => [
    'driver' => 'azure',
    'name' => env('AZURE_STORAGE_NAME'),
    'key' => env('AZURE_STORAGE_KEY'),
    'container' => env('AZURE_STORAGE_CONTAINER_NAME'),
    'url' => env('AZURE_STORAGE_URL'),
    'prefix' => null,
    'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING'),
],
Enter fullscreen mode Exit fullscreen mode

Configure your .env

Although several variables are available, the connection string alone is enough to establish the connection.

AZURE_STORAGE_CONNECTION_STRING="your-azure-storage-connection-string"
Enter fullscreen mode Exit fullscreen mode

You can find the connection string in:

Storage Account => Security + networking => Access keys


Step 2: Create an Azure Blob Storage Service

Generate a service class or controller:

cd your-app-location

sudo mkdir Services

sudo touch AzureBlobStorageService.php
Enter fullscreen mode Exit fullscreen mode

Create the following service:

<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class AzureBlobStorageService
{
    public function uploadImage($file, $path)
    {
        $fileName = uniqid()
            . Str::random(5)
            . time()
            . '.'
            . $file->getClientOriginalExtension();

        $contentType = $file->getClientMimeType();

        $options = [
            'Content-Type' => $contentType,
        ];

        Storage::disk('azure')->putFileAs(
            $path,
            $file,
            $fileName,
            $options
        );

        return Storage::disk('azure')->url("$path/$fileName");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Upload Files

Now you can inject the service into a controller or simply test it with a route.

use App\Services\AzureBlobStorageService;

Route::post('/upload-on-azure', function (
    AzureBlobStorageService $azureService
) {
    $file = request()->file('file');

    if (!$file) {
        return response()->json([
            'error' => 'No file provided',
        ], 400);
    }

    $filePath = $azureService->uploadImage($file, 'devhub');

    if (!$filePath) {
        return response()->json([
            'error' => 'Failed to upload file.',
        ], 500);
    }

    return response()->json([
        'message' => 'File uploaded successfully!',
        'file_path' => $filePath,
    ]);
});
Enter fullscreen mode Exit fullscreen mode

Step 4: Test the API

Use any API client to upload a file.

Examples:

  • Postman
  • HTTPie
  • cURL

Example using Postman:

Common Issue: 404 Resource Not Found

If the returned URL opens a 404 page or shows:

PublicAccessNotPermitted
Enter fullscreen mode Exit fullscreen mode

don't worry this is expected.

Azure Storage blocks anonymous public access by default.

Why?

Your storage account or blob container doesn't allow public blob access.

Solution 1 (Development)

Enable public access for your Blob Container.

This is acceptable for testing, but not recommended for production.


Solution 2 (Recommended)

Generate Shared Access Signature (SAS) URLs instead of exposing blobs publicly.

This provides temporary, secure access to your files while keeping your storage account private.

Example error:

Azure Portal settings:


That's It!! Can open image!

Congratulations!

Your Laravel application is now connected to Azure Blob Storage, and you can start uploading files to the cloud.

Azure Blob Storage is an excellent choice for production applications because it offers:

  • High availability
  • Scalability
  • Secure storage
  • Low maintenance
  • Integration with other Azure services

Happy coding! ❤️

If this article helped you, consider giving it a ❤️ and following me for more Laravel and Azure content.


Video Tutorial

Prefer watching a complete walkthrough?

I created a step-by-step video that covers the entire process, including:

  • Creating an Azure Storage Account
  • Creating a Blob Container
  • Getting the Storage Connection String
  • Configuring Laravel
  • Uploading files to Azure Blob Storage
  • Solving the PublicAccessNotPermitted issue
  • Testing the upload API

Watch the full tutorial here:

https://youtu.be/5XQP4Y4ZfRA?si=fMNLSfd2ViePO7i4


Connect With Me

Top comments (0)