Azure Blob Storage Operations Tutorial
This tutorial will guide you through the fundamental operations for interacting with Azure Blob Storage using C#. You'll learn how to create containers, upload and download blobs, list existing blobs, and delete them.
Prerequisites
- An Azure subscription. If you don't have one, create a free account.
- A Storage Account in your Azure subscription.
- The Azure Blob Storage client library for .NET installed in your project. You can install it via NuGet:
dotnet add package Azure.Storage.Blobs
- Basic understanding of C# and asynchronous programming.
Creating a Blob Container
A container is a logical grouping of blobs. Before you can store any data, you need to create a container within your storage account.
using Azure.Storage.Blobs;
using System;
using System.Threading.Tasks;
public class BlobStorageManager
{
private readonly string _connectionString;
public BlobStorageManager(string connectionString)
{
_connectionString = connectionString;
}
public async Task CreateContainerAsync(string containerName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
await blobServiceClient.CreateBlobContainerAsync(containerName);
Console.WriteLine($"Container '{containerName}' created.");
}
}
// Example usage:
// var manager = new BlobStorageManager("YOUR_AZURE_STORAGE_CONNECTION_STRING");
// await manager.CreateContainerAsync("my-tutorial-container");
Uploading a Blob
Blobs can be of various types, including block blobs (ideal for storing text and binary data) and append blobs (for logging scenarios). We'll focus on block blobs here.
public async Task UploadBlobAsync(string containerName, string blobName, string filePath)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.UploadBlobFromFileAsync(filePath, blobName);
Console.WriteLine($"Blob '{blobName}' uploaded to container '{containerName}'.");
}
// Example usage:
// await manager.UploadBlobAsync("my-tutorial-container", "my-document.txt", "path/to/local/document.txt");
Downloading a Blob
Retrieving a blob from Azure Blob Storage is straightforward.
public async Task DownloadBlobAsync(string containerName, string blobName, string downloadPath)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
await blobClient.DownloadToAsync(downloadPath);
Console.WriteLine($"Blob '{blobName}' downloaded to '{downloadPath}'.");
}
// Example usage:
// await manager.DownloadBlobAsync("my-tutorial-container", "my-document.txt", "path/to/save/local/document.txt");
Listing Blobs
You can easily retrieve a list of all blobs within a specific container.
public async Task ListBlobsAsync(string containerName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Console.WriteLine($"Blobs in container '{containerName}':");
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
Console.WriteLine($"- {blobItem.Name}");
}
}
// Example usage:
// await manager.ListBlobsAsync("my-tutorial-container");
Deleting a Blob
Removing blobs is a common management task.
public async Task DeleteBlobAsync(string containerName, string blobName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.DeleteBlobAsync(blobName);
Console.WriteLine($"Blob '{blobName}' deleted from container '{containerName}'.");
}
// Example usage:
// await manager.DeleteBlobAsync("my-tutorial-container", "my-document.txt");
Next Steps
Congratulations! You've learned the basics of Azure Blob Storage operations. Here are some ideas for further exploration:
- Explore other blob types (append blobs, page blobs).
- Learn about blob access tiers (Hot, Cool, Archive) for cost optimization.
- Implement blob snapshots and versions.
- Secure your blob storage with Shared Access Signatures (SAS) or Azure Active Directory.
- Integrate Blob Storage with other Azure services like Azure Functions or Azure Data Factory.
For more advanced scenarios and detailed API information, please refer to the official Azure Blob Storage documentation.