Azure Blob Storage + .NET - File Upload, Download, List, and Delete Example


To work with Azure Blob Storage in a .NET application for uploading, listing, downloading, and deleting files, follow these steps:


1. Install Required NuGet Package

Add the Azure.Storage.Blobs package to your project:

dotnet add package Azure.Storage.Blobs

2. Set Up Azure Blob Storage Client

Use the BlobServiceClient, BlobContainerClient, and BlobClient classes for interacting with Blob Storage.


3. Code Examples

Initialize the Blob Service Client

using Azure.Storage.Blobs;

// Connection string from Azure portal
string connectionString = "YourAzureBlobStorageConnectionString";
string containerName = "your-container-name";

// Create a BlobServiceClient
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Get a reference to the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

Upload a File

public async Task UploadFileAsync(string filePath)
{
    string blobName = Path.GetFileName(filePath);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    using FileStream fileStream = File.OpenRead(filePath);
    await blobClient.UploadAsync(fileStream, overwrite: true);
    Console.WriteLine($"File {blobName} uploaded successfully.");
}

List Files

public async Task ListFilesAsync()
{
    await foreach (var blobItem in containerClient.GetBlobsAsync())
    {
        Console.WriteLine($"Blob Name: {blobItem.Name}");
    }
}

Download a File

public async Task DownloadFileAsync(string blobName, string downloadFilePath)
{
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    using FileStream downloadFileStream = File.OpenWrite(downloadFilePath);
    await blobClient.DownloadToAsync(downloadFileStream);
    Console.WriteLine($"File {blobName} downloaded successfully.");
}

Delete a File

public async Task DeleteFileAsync(string blobName)
{
    BlobClient blobClient = containerClient.GetBlobClient(blobName);
    await blobClient.DeleteIfExistsAsync();
    Console.WriteLine($"File {blobName} deleted successfully.");
}

4. Putting It All Together

public class AzureBlobStorageService
{
    private readonly BlobContainerClient _containerClient;

    public AzureBlobStorageService(string connectionString, string containerName)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        _containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    }

    public async Task UploadFileAsync(string filePath)
    {
        string blobName = Path.GetFileName(filePath);
        BlobClient blobClient = _containerClient.GetBlobClient(blobName);

        using FileStream fileStream = File.OpenRead(filePath);
        await blobClient.UploadAsync(fileStream, overwrite: true);
        Console.WriteLine($"File {blobName} uploaded successfully.");
    }

    public async Task ListFilesAsync()
    {
        await foreach (var blobItem in _containerClient.GetBlobsAsync())
        {
            Console.WriteLine($"Blob Name: {blobItem.Name}");
        }
    }

    public async Task DownloadFileAsync(string blobName, string downloadFilePath)
    {
        BlobClient blobClient = _containerClient.GetBlobClient(blobName);

        using FileStream downloadFileStream = File.OpenWrite(downloadFilePath);
        await blobClient.DownloadToAsync(downloadFileStream);
        Console.WriteLine($"File {blobName} downloaded successfully.");
    }

    public async Task DeleteFileAsync(string blobName)
    {
        BlobClient blobClient = _containerClient.GetBlobClient(blobName);
        await blobClient.DeleteIfExistsAsync();
        Console.WriteLine($"File {blobName} deleted successfully.");
    }
}

5. Run the Service

static async Task Main(string[] args)
{
    string connectionString = "YourAzureBlobStorageConnectionString";
    string containerName = "your-container-name";
    AzureBlobStorageService blobService = new AzureBlobStorageService(connectionString, containerName);

    // Upload
    await blobService.UploadFileAsync("path/to/your/file.txt");

    // List
    await blobService.ListFilesAsync();

    // Download
    await blobService.DownloadFileAsync("file.txt", "path/to/save/file.txt");

    // Delete
    await blobService.DeleteFileAsync("file.txt");
}

6. Additional Notes

  • Ensure your connection string has proper permissions for the operations.
  • Handle exceptions with try-catch blocks to improve robustness.
  • For production, consider using Azure Managed Identity or Key Vault for secure credential management.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete