Azure Storage Blobs Quickstart – .NET

This tutorial shows you how to create a .NET console app that uploads, lists, downloads, and deletes blobs in Azure Blob Storage using the Azure.Storage.Blobs SDK.

Prerequisites

Setup

1

Create a new console project:

dotnet new console -n BlobQuickstart
2

Add the Azure Blob Storage SDK:

dotnet add package Azure.Storage.Blobs
3

Set environment variables for your storage account name and key (replace placeholders):

setx AZURE_STORAGE_ACCOUNT "myaccount"
setx AZURE_STORAGE_KEY "myaccountkey"

Sample Code

Save the following as Program.cs inside the project folder.

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

class Program
{
    private static readonly string connectionString = 
        $"DefaultEndpointsProtocol=https;AccountName={Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT")};AccountKey={Environment.GetEnvironmentVariable("AZURE_STORAGE_KEY")};EndpointSuffix=core.windows.net";

    private const string containerName = "quickstart-container";
    private const string blobName = "sample.txt";
    private const string localFilePath = "sample.txt";

    static async Task Main()
    {
        BlobServiceClient serviceClient = new BlobServiceClient(connectionString);
        BlobContainerClient container = await CreateContainerAsync(serviceClient);
        await UploadBlobAsync(container);
        await ListBlobsAsync(container);
        await DownloadBlobAsync(container);
        await DeleteBlobAsync(container);
    }

    private static async Task CreateContainerAsync(BlobServiceClient service)
    {
        BlobContainerClient container = service.GetBlobContainerClient(containerName);
        await container.CreateIfNotExistsAsync(PublicAccessType.None);
        Console.WriteLine($"Container '{containerName}' created (if it didn’t exist).");
        return container;
    }

    private static async Task UploadBlobAsync(BlobContainerClient container)
    {
        // create a sample file locally
        await System.IO.File.WriteAllTextAsync(localFilePath, "Hello, Azure Blob Storage!");
        BlobClient blob = container.GetBlobClient(blobName);
        await blob.UploadAsync(localFilePath, overwrite: true);
        Console.WriteLine($"Uploaded blob '{blobName}'.");
    }

    private static async Task ListBlobsAsync(BlobContainerClient container)
    {
        Console.WriteLine("Listing blobs in container:");
        await foreach (BlobItem blobItem in container.GetBlobsAsync())
        {
            Console.WriteLine($" - {blobItem.Name}");
        }
    }

    private static async Task DownloadBlobAsync(BlobContainerClient container)
    {
        BlobClient blob = container.GetBlobClient(blobName);
        string downloadPath = "downloaded_" + blobName;
        await blob.DownloadToAsync(downloadPath);
        Console.WriteLine($"Downloaded blob to '{downloadPath}'.");
    }

    private static async Task DeleteBlobAsync(BlobContainerClient container)
    {
        BlobClient blob = container.GetBlobClient(blobName);
        await blob.DeleteIfExistsAsync();
        Console.WriteLine($"Deleted blob '{blobName}'.");
        await container.DeleteIfExistsAsync();
        Console.WriteLine($"Deleted container '{containerName}'.");
    }
}

Run the Application

dotnet run

You should see output similar to:

Container 'quickstart-container' created (if it didn’t exist).
Uploaded blob 'sample.txt'.
Listing blobs in container:
 - sample.txt
Downloaded blob to 'downloaded_sample.txt'.
Deleted blob 'sample.txt'.
Deleted container 'quickstart-container'.

Next Steps