Azure Blob Storage is one of Microsoft Azure’s storage solutions. Think of it as a massive cupboard in the cloud where you can keep a lot of stuff, from pictures and videos to logs and backups. In this article, we’ll explore what Azure Blob Storage is, when you might want to use it, and how to play with it using C#.
Azure Blob Storage is a place in the Azure cloud where you can save large amounts of unstructured data, which means it doesn’t have a specific format or structure. It’s like having a big box where you can throw in anything you want and then easily find and use it later.
When Would You Use Azure Blob Storage?
- Keeping Pictures and Videos: Maybe you have an app or website where users upload photos and videos. Azure Blob can hold them.
- Storing Backups: If you want to back up data from your apps or websites, Azure Blob is a safe place for that.
- Holding Log Files: If your apps create logs to track what’s happening, you can send them to Azure Blob for storage.
- Delivering Big Files: Let’s say you want users to download large files from your site. Azure Blob can host and deliver them.
Microsoft offers a C# library to make it easier to connect your C# apps to Azure Blob Storage. This means you can manage your blobs (the items you store) directly from your C# code.
Getting Started with C# and Azure Blob
First, add the Azure Blob library to your C# project. You can do this using NuGet:
Install-Package Azure.Storage.Blobs
Connecting to Your Blob Storage
BlobServiceClient blobServiceClient = new BlobServiceClient("your_connection_string_here"); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("your_container_name");
Adding (Uploading) a File
BlobClient blobClient = containerClient.GetBlobClient("your_blob_name");
using FileStream uploadFileStream = File.OpenRead("local_file_path"); blobClient.Upload(uploadFileStream, true);
uploadFileStream.Close();
Getting (Downloading) a File
BlobClient blobClient = containerClient.GetBlobClient("your_blob_name");
BlobDownloadInfo download = blobClient.Download();
using (FileStream fs = File.OpenWrite("local_file_path")) { download.Content.CopyTo(fs); fs.Close(); }
Changing a Blob’s Name (Rename)
Well, Azure Blob Storage doesn’t let you directly rename a blob. But a workaround is to copy the blob to a new name and then delete the old one.
Removing (Deleting) a File
BlobClient blobClient = containerClient.GetBlobClient("your_blob_name"); blobClient.DeleteIfExists();
Suleyman Cabir Ataman, PhD