How to Use Azure Blob Storage
This guide provides practical examples and step-by-step instructions on how to interact with Azure Blob Storage using various tools and SDKs. We'll cover common operations like uploading, downloading, listing, and managing blobs.
Prerequisites
Before you begin, ensure you have the following:
- An Azure subscription. If you don't have one, create a free account.
- A storage account. You can create one through the Azure portal.
- The connection string or access key for your storage account.
1. Using the Azure Portal
The Azure portal offers a user-friendly graphical interface for managing your blobs.
- Navigate to your storage account in the Azure portal.
- Under "Data storage", select "Containers".
- Click "+ Container" to create a new container.
- Select a container and click "Upload" to upload a new blob.
- You can click on a blob to view its properties, download it, or delete it.
2. Using Azure CLI
The Azure Command-Line Interface (CLI) is a powerful tool for scripting and automating storage operations.
a) Login to Azure
az login
b) Set Your Subscription (if needed)
az account set --subscription ""
c) Upload a Blob
Upload a file named my-local-file.txt to a container named mycontainer.
az storage blob upload \
--account-name \
--container-name mycontainer \
--name my-remote-file.txt \
--file my-local-file.txt \
--connection-string ""
d) Download a Blob
Download a blob named my-remote-file.txt from mycontainer to downloaded-file.txt.
az storage blob download \
--account-name \
--container-name mycontainer \
--name my-remote-file.txt \
--file downloaded-file.txt \
--connection-string ""
e) List Blobs in a Container
az storage blob list \
--account-name \
--container-name mycontainer \
--connection-string ""
3. Using Azure Storage SDKs
Azure provides SDKs for various programming languages to interact with Blob Storage programmatically. Here are examples using Python and .NET.
a) Python SDK
First, install the SDK:
pip install azure-storage-blob
Example: Uploading a Blob
Python Upload Example
from azure.storage.blob import BlobServiceClient
connect_str = ""
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = "mycontainer"
local_file_name = "local_upload.txt"
blob_name = "remote_upload.txt"
try:
# Create a blob client using the container name
container_client = blob_service_client.get_container_client(container_name)
# Create a local file for upload
with open(local_file_name, "w") as my_file:
my_file.write("This is a test file for Azure Blob Storage upload.")
# Upload the blob
with open(local_file_name, "rb") as data:
container_client.upload_blob(name=blob_name, data=data)
print(f"Blob '{blob_name}' uploaded successfully.")
except Exception as ex:
print('Exception:')
print(ex)
b) .NET SDK
First, install the NuGet package:
Install-Package Azure.Storage.Blobs
Example: Downloading a Blob
C# Download Example
using Azure.Storage.Blobs;
using System;
using System.IO;
public class BlobDownloader
{
public static void DownloadBlob(string connectionString, string containerName, string blobName, string localFilePath)
{
try
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
using (FileStream downloadFileStream = File.OpenWrite(localFilePath))
{
Response<Response> downloadResult = blobClient.DownloadTo(downloadFileStream);
Console.WriteLine($"Blob '{blobName}' downloaded successfully to '{localFilePath}'.");
}
}
catch (RequestFailedException e)
{
Console.WriteLine($"Error downloading blob: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"An unexpected error occurred: {e.Message}");
}
}
public static void Main(string[] args)
{
string connectionString = "";
string containerName = "mycontainer";
string blobName = "remote_upload.txt";
string localFilePath = "downloaded_from_azure.txt";
DownloadBlob(connectionString, containerName, blobName, localFilePath);
}
}
Key Concepts
- Blobs: Unstructured data like text or binary data.
- Containers: A logical grouping of blobs. Similar to directories.
- Storage Account: A unique namespace in Azure for object replication. All Azure Storage objects are part of a storage account.