This quickstart demonstrates how to create a .NET console app that uploads, lists, and downloads blobs from an Azure Storage account.
Prerequisites
- Azure subscription (free trial works)
- Visual Studio 2022 or later (or
dotnet
CLI) - .NET 6.0 SDK or later
- Azure Storage account with a Blob container
Step 1 – Create a .NET console app
Run the following commands
dotnet new console -n BlobQuickstart
cd BlobQuickstart
dotnet add package Azure.Storage.Blobs
dotnet restore
Step 2 – Add code to upload, list, and download blobs
Replace Program.cs
with:
using System;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
class Program
{
// Set these values before running
private const string connectionString = ""YOUR_STORAGE_CONNECTION_STRING"";
private const string containerName = ""quickstart-blobs"";
static async Task Main()
{
BlobServiceClient serviceClient = new(connectionString);
BlobContainerClient containerClient = serviceClient.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync(PublicAccessType.Blob);
// Upload a file
string localPath = Path.Combine(Environment.CurrentDirectory, ""sample.txt"");
await File.WriteAllTextAsync(localPath, ""Hello, Azure Blob Storage!"");
BlobClient blob = containerClient.GetBlobClient(""sample.txt"");
await blob.UploadAsync(localPath, overwrite:true);
Console.WriteLine($""Uploaded: {blob.Uri}"");
// List blobs
Console.WriteLine(""Listing blobs in container:"");
await foreach (BlobItem item in containerClient.GetBlobsAsync())
{
Console.WriteLine($"" - {item.Name}"");
}
// Download the blob
string downloadPath = Path.Combine(Environment.CurrentDirectory, ""downloaded-sample.txt"");
await blob.DownloadToAsync(downloadPath);
Console.WriteLine($""Downloaded to: {downloadPath}"");
}
}
Step 3 – Run the application
dotnet run
The console will display upload, list, and download operations. Check your Azure portal to see the uploaded blob.
Next steps
- Explore Shared Access Signatures (SAS) for secure delegated access.
- Implement asynchronous uploads for large files.
- Use soft delete to protect against accidental deletion.