Introduction

In modern cloud-based applications, storing files securely and scalably is essential. Whether it's profile images, invoices, PDFs, or logs — cloud storage is the go-to solution.

In this article, we will learn how to:

We will use:

What is Azure Blob Storage?

Microsoft provides a service called Azure Blob Storage—a massively scalable object storage solution for storing unstructured data in the cloud.

Unstructured data means data that does not fit into traditional relational databases, such as

Understanding Object Storage

Top-level resource in Azure.
Acts like a global namespace for your data.

Similar to a folder (logical grouping of blobs).

Actual file stored in Azure.

Example:

https://mystorage.blob.core.windows.net/invoices/invoice1.pdf

Types of Azure Blob Storage

Block blobs store data as blocks, which are uploaded independently and then committed together.

Each block:

Best For: Images, Videos, Documents

Append blobs are optimized for append operations only.

Best For: Logging systems, Audit trails

Page blobs store data in 512-byte pages.

Best For: Virtual machine disks (VHD files), Databases

Create Azure Blob Storage

Go to Azure Portal → Create Resource → Storage Account

Important Settings

S1S2S3

After creation:

  1. Go to Containers

  2. Create a new container

  3. Set access level (Private recommended)

s5

Configure it in a .NET 8 Web API

Get Connection String

Go to:

Storage Account → Access Keys → Copy Connection String

s7

Never hardcode in production. Use:

Create .NET 8 Web API

Install NuGet package:

dotnet add package Azure.Storage.Blobs

Add Connection String in appsettings.json

{
  "AzureBlobStorage": {
    "ConnectionString": "YOUR_CONNECTION_STRING",
    "ContainerName": "uploads"
  }
}

Create Blob Service : BlobService.cs

using Azure.Storage.Blobs;

public class BlobService
{
    private readonly BlobContainerClient _containerClient;

    public BlobService(IConfiguration configuration)
    {
        var connectionString = configuration["AzureBlobStorage:ConnectionString"];
        var containerName = configuration["AzureBlobStorage:ContainerName"];

        var blobServiceClient = new BlobServiceClient(connectionString);
        _containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    }

    public async Task<string> UploadFileAsync(IFormFile file)
    {
        var blobClient = _containerClient.GetBlobClient(file.FileName);

        using (var stream = file.OpenReadStream())
        {
            await blobClient.UploadAsync(stream, overwrite: true);
        }

        return blobClient.Uri.ToString();
    }
}

Register Service in Program.cs

builder.Services.AddScoped<BlobService>();

Create Controller

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class UploadController : ControllerBase
{
    private readonly BlobService _blobService;

    public UploadController(BlobService blobService)
    {
        _blobService = blobService;
    }

    [HttpPost]
    public async Task<IActionResult> Upload(IFormFile file)
    {
        if (file == null || file.Length == 0)
            return BadRequest("File not selected");

        var result = await _blobService.UploadFileAsync(file);
        return Ok(new { FileUrl = result });
    }
}

Test using Swagger

Run the application: Open Swagger → Upload file → Click Execute

s8s9

Conclusion

Uploading files to Azure Blob Storage using .NET 8 is:

This approach works perfectly with:

Thank you so much for reading this article.

Happy coding!!