How to Use Azure Blob Storage with .NET: Simple Guide
To use
Azure Blob Storage with .NET, install the Azure.Storage.Blobs package, create a BlobServiceClient with your storage connection string, then use it to upload or download blobs. This lets your .NET app store and retrieve files in the cloud easily.Syntax
Here is the basic syntax to connect and upload a file to Azure Blob Storage using .NET:
BlobServiceClient: Connects to your storage account.BlobContainerClient: Represents a container (like a folder) in blob storage.BlobClient: Represents a single blob (file) inside the container.UploadAsync: Uploads a file stream to the blob.
csharp
using Azure.Storage.Blobs; using System.IO; string connectionString = "<your_connection_string>"; string containerName = "mycontainer"; string blobName = "myfile.txt"; string localFilePath = "path/to/local/file.txt"; // Create a BlobServiceClient BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); // Get a container client BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); // Get a blob client BlobClient blobClient = containerClient.GetBlobClient(blobName); // Upload the file using FileStream uploadFileStream = File.OpenRead(localFilePath); await blobClient.UploadAsync(uploadFileStream, overwrite: true); uploadFileStream.Close();
Example
This example shows how to upload a text file to Azure Blob Storage and then download it back to verify the content.
csharp
using System; using System.IO; using System.Threading.Tasks; using Azure.Storage.Blobs; class Program { static async Task Main() { string connectionString = "<your_connection_string>"; string containerName = "sample-container"; string blobName = "example.txt"; string localUploadPath = "example_upload.txt"; string localDownloadPath = "example_download.txt"; // Write sample content to upload file await File.WriteAllTextAsync(localUploadPath, "Hello Azure Blob Storage!"); // Create BlobServiceClient BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); // Create container if not exists BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); await containerClient.CreateIfNotExistsAsync(); // Get BlobClient BlobClient blobClient = containerClient.GetBlobClient(blobName); // Upload file using (FileStream uploadFileStream = File.OpenRead(localUploadPath)) { await blobClient.UploadAsync(uploadFileStream, overwrite: true); } Console.WriteLine($"Uploaded {blobName} to container {containerName}."); // Download blob to local file using (FileStream downloadFileStream = File.OpenWrite(localDownloadPath)) { await blobClient.DownloadToAsync(downloadFileStream); } string downloadedContent = await File.ReadAllTextAsync(localDownloadPath); Console.WriteLine("Downloaded content: " + downloadedContent); } }
Output
Uploaded example.txt to container sample-container.
Downloaded content: Hello Azure Blob Storage!
Common Pitfalls
Common mistakes when using Azure Blob Storage with .NET include:
- Not setting
overwrite: truewhen uploading, causing errors if the blob exists. - Using incorrect or missing connection strings.
- Not creating the container before uploading blobs.
- Forgetting to dispose streams properly, leading to file locks.
Always handle exceptions and validate your storage credentials.
csharp
/* Wrong: Upload without overwrite - throws if blob exists */ await blobClient.UploadAsync(uploadFileStream); /* Right: Upload with overwrite enabled */ await blobClient.UploadAsync(uploadFileStream, overwrite: true);
Quick Reference
Here is a quick cheat sheet for common Azure Blob Storage operations in .NET:
| Operation | Method | Description |
|---|---|---|
| Connect to Storage | new BlobServiceClient(connectionString) | Create client to access storage account |
| Get Container | blobServiceClient.GetBlobContainerClient(containerName) | Access a container in storage |
| Create Container | containerClient.CreateIfNotExistsAsync() | Create container if missing |
| Get Blob | containerClient.GetBlobClient(blobName) | Access a specific blob (file) |
| Upload Blob | blobClient.UploadAsync(stream, overwrite: true) | Upload or overwrite a blob |
| Download Blob | blobClient.DownloadToAsync(stream) | Download blob content to stream |
| Delete Blob | blobClient.DeleteIfExistsAsync() | Delete blob if it exists |
Key Takeaways
Install and use the Azure.Storage.Blobs package to work with blob storage in .NET.
Always create the container before uploading blobs to avoid errors.
Use overwrite:true when uploading to replace existing blobs safely.
Dispose file streams properly to avoid file locks and resource leaks.
Validate your connection string and handle exceptions for reliable storage access.