Accessing Azure Storage Blobs
This article describes how to access blob data in Azure Storage. Azure Blob Storage is a cloud object storage solution for the modern data requirements, ranging from serving images and documents directly to applications, to streaming video and audio, to writing to log files.
Note: For comprehensive examples and API references, please refer to the Azure Storage client library documentation.
Methods of Accessing Blobs
You can access blobs in your Azure Storage account through several methods, each suited to different scenarios:
1. Azure Portal
The Azure portal provides a graphical interface for managing your storage account and its contents. You can upload, download, list, and delete blobs directly through the portal.
To access blobs via the portal:
- Navigate to your storage account in the Azure portal.
- Select Blob service > Containers.
- Click on a container, then select the blob you wish to access.
- Use the available options to download, delete, or manage the blob.
2. Azure Storage Explorer
Azure Storage Explorer is a cross-platform graphical tool for managing your Azure Storage resources. It offers a more advanced interface than the Azure portal for interacting with blobs, including drag-and-drop functionality for uploads and downloads.
Download Azure Storage Explorer from here.
3. Azure Storage Client Libraries
For programmatic access, Azure provides robust client libraries for various programming languages including .NET, Java, Python, Node.js, and Go. These libraries allow you to interact with blob storage from your applications.
Example: Uploading a blob with .NET SDK
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.IO;
using System.Threading.Tasks;
public class BlobUploader
{
public static async Task UploadFileAsync(string connectionString, string containerName, string blobName, string filePath)
{
// Get the connection string from app settings or environment variables.
// BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
//
// // Use the connection string to create a BlobServiceClient
var blobServiceClient = new BlobServiceClient(connectionString);
// Get a reference to a container client
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Create the container if it doesn't exist
await containerClient.CreateIfNotExistsAsync();
// Get a block blob client reference
BlobClient blobClient = containerClient.GetBlobClient(blobName);
Console.WriteLine("Uploading to blob storage as '{0}'", blobClient.Name);
using FileStream uploadFileStream = File.OpenRead(filePath);
await blobClient.UploadAsync(uploadFileStream, true);
Console.WriteLine("Uploaded blob '{0}' to container '{1}'", blobName, containerName);
}
}
Example: Downloading a blob with Python SDK
from azure.storage.blob import BlobServiceClient
def download_blob(connect_str, container_name, blob_name, download_path):
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Get a client to the container
container_client = blob_service_client.get_container_client(container_name)
# Get a client to the blob
blob_client = container_client.get_blob_client(blob_name)
print(f"Downloading blob '{blob_name}' from container '{container_name}'")
with open(download_path, "wb") as download_file:
download_stream = blob_client.download_blob()
download_file.write(download_stream.readall())
print(f"Downloaded blob to '{download_path}'")
# Example usage:
# connection_string = "YOUR_CONNECTION_STRING"
# container = "your-container-name"
# blob = "your-blob-name.txt"
# download_file_path = "path/to/save/downloaded_file.txt"
# download_blob(connection_string, container, blob, download_file_path)
4. Azure CLI
The Azure Command-Line Interface (CLI) provides a powerful set of commands for managing Azure resources, including Blob Storage. You can use the CLI to upload, download, list, and delete blobs from your terminal.
Example: Uploading a blob with Azure CLI
az storage blob upload \
--account-name mystorageaccount \
--container-name mycontainer \
--name myblob.txt \
--file mylocalfile.txt \
--auth-mode login
Example: Downloading a blob with Azure CLI
az storage blob download \
--account-name mystorageaccount \
--container-name mycontainer \
--name myblob.txt \
--file mylocaldownloadedfile.txt \
--auth-mode login
Tip: For efficient scripting and automation, the Azure CLI and client libraries are highly recommended.
5. REST API
Azure Storage exposes a REST API that allows you to interact with Blob Storage services programmatically. This is the underlying interface used by the client libraries and is useful for scenarios where you need fine-grained control or are integrating with environments that don't have native client libraries.
Refer to the Azure Storage REST API documentation for details.
Securing Blob Access
Access to blobs can be secured using various mechanisms:
- Access Keys: Primary and secondary keys that provide full access to the storage account. Use with caution.
- Shared Access Signatures (SAS): Delegated access with specific permissions and time constraints.
- Azure Active Directory (Azure AD) integration: For fine-grained access control using roles and permissions.
Learn more about securing your blob data in the Secure Azure Blob Storage article.
Warning: Never expose your storage account access keys directly in client-side code or publicly accessible repositories.