How to Use Azure Blob Storage
This guide provides step-by-step instructions for common tasks related to Azure Blob Storage. Azure Blob Storage is Microsoft's object storage solution for the cloud. It's optimized for storing massive amounts of unstructured data.
1. Creating a Storage Account
Before you can use Blob Storage, you need to create an Azure Storage account. You can do this through the Azure portal, Azure CLI, or Azure PowerShell.
Azure Portal
- Sign in to the Azure portal.
- Click Create a resource.
- Search for "Storage account" and select it.
- Click Create.
- Fill in the required details, including subscription, resource group, storage account name (globally unique), region, and performance tier. For Blob storage, choose "Standard performance".
- Select the redundancy option (e.g., LRS, GRS).
- Review and create the storage account.
Azure CLI
az storage account create \
--name mystorageaccountname \
--resource-group myresourcegroup \
--location eastus \
--sku Standard_LRS \
--kind StorageV2
2. Creating a Blob Container
Containers are used to organize blobs within a storage account. Think of them like folders.
Azure Portal
- Navigate to your storage account in the Azure portal.
- Under "Data storage", select Containers.
- Click + Container.
- Enter a name for your container (e.g.,
mycontainer). - Choose a public access level (e.g., Private, Blob, Container).
- Click Create.
Azure CLI
az storage container create \
--name mycontainer \
--account-name mystorageaccountname \
--auth-mode login
Replace mystorageaccountname with your actual storage account name.
3. Uploading Blobs
You can upload various types of data as blobs, including documents, images, videos, and backups.
Azure Portal
- Navigate to your container.
- Click Upload.
- Select the files you want to upload.
- Choose an upload type (Block Blob, Append Blob, Page Blob). Block blobs are the most common.
- Click Upload.
Azure CLI
To upload a block blob:
az storage blob upload \
--account-name mystorageaccountname \
--container-name mycontainer \
--name myblob.txt \
--file /path/to/your/local/myblob.txt \
--auth-mode login
4. Downloading Blobs
Retrieve your data from Blob Storage.
Azure Portal
- Navigate to your container.
- Click on the blob you want to download.
- Click the Download button.
Azure CLI
az storage blob download \
--account-name mystorageaccountname \
--container-name mycontainer \
--name myblob.txt \
--file ./local-download/myblob.txt \
--auth-mode login
5. Deleting Blobs and Containers
Manage your storage by deleting unneeded data.
Azure CLI (Delete Blob)
az storage blob delete \
--account-name mystorageaccountname \
--container-name mycontainer \
--name myblob.txt \
--auth-mode login
Azure CLI (Delete Container)
az storage container delete \
--account-name mystorageaccountname \
--name mycontainer \
--auth-mode login
6. Accessing Blobs Programmatically
Azure Blob Storage can be accessed using various SDKs for different programming languages.
Using Azure Storage SDK for .NET
Install the NuGet package:
dotnet add package Azure.Storage.Blobs
Example C# code:
using Azure.Storage.Blobs;
string connectionString = "YOUR_CONNECTION_STRING";
string containerName = "mycontainer";
string blobName = "my-new-blob.txt";
string filePath = "local-file-to-upload.txt";
// Get a client object for the blob
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Upload the blob
using FileStream uploadFileStream = File.OpenRead(filePath);
await blobClient.UploadAsync(uploadFileStream, true); // Overwrite if exists
Console.WriteLine($"Blob '{blobName}' uploaded successfully.");
For more advanced scenarios like managing access tiers, lifecycle management, or using different blob types, refer to the official Azure Storage documentation.
Learn More on Microsoft Docs Blob Storage Concepts