Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Azure Blob Storage Integration in ASP.NET Core - File Management at Scale

Azure Blob Storage Integration in ASP.NET Core - File Management at Scale

Date- Apr 20,2026 81
azure blob storage

Overview

Azure Blob Storage is a service offered by Microsoft Azure that allows developers to store vast amounts of unstructured data in a highly scalable way. This service is particularly useful for applications that handle large files, such as images, videos, and documents, as it provides a cost-effective solution for data storage that can grow with the application's needs. By utilizing Azure Blob Storage, developers can offload file management from their application servers, thereby enhancing performance and reliability.

The primary problem that Azure Blob Storage addresses is the challenge of managing large volumes of data efficiently. Traditional storage solutions can be cumbersome and expensive, especially in scenarios where data access patterns are unpredictable. Azure Blob Storage solves this by providing a RESTful API, allowing seamless integration with various applications, including ASP.NET Core. Real-world use cases include content delivery networks (CDNs), backup solutions, and web applications that require file uploads and downloads.

Prerequisites

  • Basic understanding of ASP.NET Core: Familiarity with creating web applications using ASP.NET Core.
  • An Azure account: You need an Azure subscription to create and manage Blob Storage resources.
  • Visual Studio or Visual Studio Code: An IDE to write and test your ASP.NET Core application.
  • NuGet package manager: Knowledge of how to add packages to your project.

Setting Up Azure Blob Storage

Before integrating Azure Blob Storage into your ASP.NET Core application, you need to set up a Blob Storage account in the Azure portal. This involves creating a storage account and configuring access keys. The storage account is a unique namespace for your data in Azure, and it provides the ability to manage data across various Blob containers.

The creation of a storage account can be done through the Azure portal by navigating to the 'Storage accounts' section and clicking on 'Add'. You'll need to specify the subscription, resource group, storage account name, region, and performance options. Once the account is created, you can access the storage account keys from the 'Access keys' section, which will be necessary for authenticating your application.

// Configuration setup in appsettings.json for Azure Blob Storage
{
  "AzureBlobStorage": {
    "AccountName": "your_account_name",
    "AccountKey": "your_account_key",
    "ContainerName": "your_container_name"
  }
}

This JSON configuration stores the necessary credentials for connecting to the Azure Blob Storage account. Make sure to replace the placeholders with your actual account name and key. The ContainerName is where your files will be stored.

Accessing Configuration in ASP.NET Core

To access the configuration settings in your ASP.NET Core application, you can inject the IConfiguration service into your services. This allows you to retrieve the necessary credentials for Azure Blob Storage.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.Configure<BlobStorageOptions>(Configuration.GetSection("AzureBlobStorage"));
}

This code snippet shows how to bind the Azure Blob Storage configuration section to a BlobStorageOptions class, which can be defined as follows:

// BlobStorageOptions.cs
public class BlobStorageOptions
{
    public string AccountName { get; set; }
    public string AccountKey { get; set; }
    public string ContainerName { get; set; }
}

By using the Configure method in Startup.cs, you ensure that the configuration settings are available throughout the application.

Uploading Files to Azure Blob Storage

Uploading files to Azure Blob Storage involves creating a Blob client and then using that client to upload files to the specified container. This process is straightforward and can be achieved using the Azure.Storage.Blobs library, which you can install via NuGet.

// FileUploadController.cs
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

public class FileUploadController : Controller
{
    private readonly BlobServiceClient _blobServiceClient;
    private readonly string _containerName;

    public FileUploadController(IOptions<BlobStorageOptions> options)
    {
        _blobServiceClient = new BlobServiceClient(new StorageSharedKeyCredential(options.Value.AccountName, options.Value.AccountKey));
        _containerName = options.Value.ContainerName;
    }

    [HttpPost]
    public async Task<IActionResult> UploadFile(IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            return BadRequest("No file uploaded.");
        }

        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        await containerClient.CreateIfNotExistsAsync();

        var blobClient = containerClient.GetBlobClient(file.FileName);
        using (var stream = file.OpenReadStream())
        {
            await blobClient.UploadAsync(stream, true);
        }

        return Ok("File uploaded successfully.");
    }
}

This code defines a controller for handling file uploads. It uses dependency injection to access the configuration options for Azure Blob Storage. The UploadFile method checks if a file has been uploaded, creates a Blob container if it doesn't exist, and uploads the file stream to the specified Blob.

Line-by-Line Explanation

  • BlobServiceClient: This client is responsible for interacting with the Blob service.
  • GetBlobContainerClient: Retrieves a reference to the container specified.
  • CreateIfNotExistsAsync: Ensures that the container exists before uploading the file.
  • GetBlobClient: Obtains a reference to a specific Blob within the container.
  • UploadAsync: Asynchronously uploads the file stream to the Blob storage.

Downloading Files from Azure Blob Storage

Downloading files from Azure Blob Storage is as straightforward as uploading them. You need to create a Blob client and then retrieve the Blob content as a stream. This allows you to serve files directly to users or to process them further.

// FileDownloadController.cs
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

public class FileDownloadController : Controller
{
    private readonly BlobServiceClient _blobServiceClient;
    private readonly string _containerName;

    public FileDownloadController(IOptions<BlobStorageOptions> options)
    {
        _blobServiceClient = new BlobServiceClient(new StorageSharedKeyCredential(options.Value.AccountName, options.Value.AccountKey));
        _containerName = options.Value.ContainerName;
    }

    [HttpGet]
    public async Task<IActionResult> DownloadFile(string fileName)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var blobClient = containerClient.GetBlobClient(fileName);

        if (!await blobClient.ExistsAsync())
        {
            return NotFound();
        }

        var memoryStream = new MemoryStream();
        await blobClient.DownloadToAsync(memoryStream);
        memoryStream.Position = 0;

        return File(memoryStream, "application/octet-stream", fileName);
    }
}

In this example, the DownloadFile method retrieves a specified Blob and checks for its existence. If found, it downloads the Blob content into a MemoryStream and returns it as a file response to the client.

Line-by-Line Explanation

  • ExistsAsync: Checks if the specified Blob exists in the container.
  • DownloadToAsync: Downloads the Blob content into a stream.
  • File: Returns the stream as a file response, allowing users to download it.

Managing Blobs - Listing and Deleting

Managing Blobs in Azure involves listing and deleting operations. Listing allows you to fetch all Blobs in a container, while deleting is essential for removing unnecessary files and managing storage costs.

// BlobManagementController.cs
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

public class BlobManagementController : Controller
{
    private readonly BlobServiceClient _blobServiceClient;
    private readonly string _containerName;

    public BlobManagementController(IOptions<BlobStorageOptions> options)
    {
        _blobServiceClient = new BlobServiceClient(new StorageSharedKeyCredential(options.Value.AccountName, options.Value.AccountKey));
        _containerName = options.Value.ContainerName;
    }

    [HttpGet]
    public async Task<IActionResult> ListBlobs()
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var blobList = new List<string>();

        await foreach (var blobItem in containerClient.GetBlobsAsync())
        {
            blobList.Add(blobItem.Name);
        }

        return Ok(blobList);
    }

    [HttpDelete]
    public async Task<IActionResult> DeleteBlob(string fileName)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var blobClient = containerClient.GetBlobClient(fileName);

        await blobClient.DeleteIfExistsAsync();
        return Ok("Blob deleted successfully.");
    }
}

This code manages Blob operations by providing endpoints for listing and deleting Blobs. The ListBlobs method fetches all Blobs in the container, while the DeleteBlob method deletes a specified Blob if it exists.

Line-by-Line Explanation

  • GetBlobsAsync: Asynchronously retrieves all Blobs from the container.
  • DeleteIfExistsAsync: Deletes the Blob only if it exists, preventing errors.

Edge Cases & Gotchas

While integrating Azure Blob Storage, it's essential to be mindful of certain edge cases and potential pitfalls.

Handling Large Files

When uploading large files, ensure that you manage memory effectively. Streaming uploads can help mitigate memory issues, and using UploadAsync with a stream is recommended.

// Incorrect approach: Loading the entire file into memory
var fileBytes = await file.GetBytesAsync(); // Inefficient for large files

Instead, use the streaming approach demonstrated earlier to prevent memory overflow.

Managing Concurrent Uploads

When multiple users upload files simultaneously, ensure that your application can handle concurrent requests. Azure Blob Storage supports this inherently, but your application must properly manage state and errors.

Performance & Best Practices

Optimizing performance when using Azure Blob Storage involves several strategies.

Use Asynchronous Operations

Always utilize asynchronous methods for file uploads and downloads to avoid blocking the main thread, especially in web applications.

// Asynchronous uploads and downloads
await blobClient.UploadAsync(stream, true);

Asynchronous operations improve application responsiveness and user experience.

Utilize Blob Lifecycle Management

Azure provides lifecycle management policies that automatically transition Blobs to cooler storage tiers or delete them after a specified time. This can significantly reduce costs for infrequently accessed data.

Real-World Scenario

Let’s consider a real-world scenario where we build a simple document management system. This application allows users to upload, download, list, and delete documents stored in Azure Blob Storage.

// DocumentManagementController.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class DocumentManagementController : Controller
{
    private readonly BlobServiceClient _blobServiceClient;
    private readonly string _containerName;

    public DocumentManagementController(IOptions<BlobStorageOptions> options)
    {
        _blobServiceClient = new BlobServiceClient(new StorageSharedKeyCredential(options.Value.AccountName, options.Value.AccountKey));
        _containerName = options.Value.ContainerName;
    }

    [HttpPost]
    public async Task<IActionResult> UploadDocument(IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            return BadRequest("No file uploaded.");
        }

        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        await containerClient.CreateIfNotExistsAsync();

        var blobClient = containerClient.GetBlobClient(file.FileName);
        using (var stream = file.OpenReadStream())
        {
            await blobClient.UploadAsync(stream, true);
        }

        return Ok("Document uploaded successfully.");
    }

    [HttpGet]
    public async Task<IActionResult> ListDocuments()
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var documents = new List<string>();

        await foreach (var blobItem in containerClient.GetBlobsAsync())
        {
            documents.Add(blobItem.Name);
        }

        return Ok(documents);
    }

    [HttpGet]
    public async Task<IActionResult> DownloadDocument(string fileName)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var blobClient = containerClient.GetBlobClient(fileName);

        if (!await blobClient.ExistsAsync())
        {
            return NotFound();
        }

        var memoryStream = new MemoryStream();
        await blobClient.DownloadToAsync(memoryStream);
        memoryStream.Position = 0;

        return File(memoryStream, "application/octet-stream", fileName);
    }

    [HttpDelete]
    public async Task<IActionResult> DeleteDocument(string fileName)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
        var blobClient = containerClient.GetBlobClient(fileName);

        await blobClient.DeleteIfExistsAsync();
        return Ok("Document deleted successfully.");
    }
}

This controller encapsulates all the file management functionalities required for a document management system. Users can upload documents, list all documents, download specific documents, and delete documents as needed.

Conclusion

  • Azure Blob Storage provides a scalable solution for managing unstructured data in ASP.NET Core applications.
  • Asynchronous file operations improve performance and user experience.
  • Implementing proper error handling and edge case management is essential for robust applications.
  • Utilizing Blob lifecycle management can significantly reduce storage costs.
  • Real-world scenarios help solidify understanding and practical implementation of concepts.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating Azure Cognitive Search into ASP.NET Core Applications
May 08, 2026
Secure File Management: Google Drive Integration in ASP.NET Core
Apr 14, 2026
Integrating MinIO Object Storage in ASP.NET Core: A Self-Hosted S3 Alternative
May 03, 2026
Integrating Backblaze B2 Cloud Storage with ASP.NET Core Applications
May 03, 2026
Previous in ASP.NET Core
AWS S3 File Upload Integration in ASP.NET Core - Upload, Download…
Next in ASP.NET Core
Integrating Google Cloud Storage with ASP.NET Core: A Comprehensi…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Complete Guide to C++ Classes: Explained with Examples 4,212 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 367 views
  • 3
    Create Database and CRUD operation 3,388 views
  • 4
    Mastering TypeScript Utility Types: Partial, Required, Rea… 675 views
  • 5
    Responsive Slick Slider 23,373 views
  • 6
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 156 views
  • 7
    Integrating Anthropic Claude API in ASP.NET Core for AI Ch… 141 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26191 views
  • Exception Handling Asp.Net Core 20938 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20391 views
  • How to implement Paypal in Asp.Net Core 19753 views
  • Task Scheduler in Asp.Net core 17705 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor