Azure Blob Storage is a service that stores unstructured data such as text or binary data. Blobs can be any type of text or binary data, such as images, documents, or streaming media. A single Azure Storage account can store hundreds of terabytes of data.
This document guides you through the process of uploading files (blobs) to Azure Blob Storage and then retrieving them. We'll cover common scenarios and provide code examples using the Azure SDK for .NET and Azure CLI.
You can upload blobs to Azure Blob Storage using various methods. Here are two common ones:
The Azure CLI provides a simple way to upload files. First, ensure you are logged into your Azure account:
az login
Then, use the az storage blob upload command:
az storage blob upload \
--account-name \
--account-key \
--container-name \
--name \
--file
Replace placeholders like <YOUR_STORAGE_ACCOUNT_NAME>, <YOUR_STORAGE_ACCOUNT_KEY>, <YOUR_CONTAINER_NAME>, <DESTINATION_BLOB_NAME>, and <LOCAL_FILE_PATH> with your actual values.
You can find your storage account name and key in the Azure portal under your storage account's "Access keys" section.
This example demonstrates uploading a file using the Azure Blob Storage SDK for .NET. Make sure you have the Azure.Storage.Blobs NuGet package installed.
using Azure.Storage.Blobs;
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)
{
// Create a BlobServiceClient object using the connection string
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Get a reference to the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Get a reference to the blob
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Open the file to be uploaded
using (FileStream uploadFileStream = File.OpenRead(filePath))
{
// Upload the file
await blobClient.UploadAsync(uploadFileStream, true); // Overwrite if it already exists
Console.WriteLine($"Successfully uploaded {blobName} to container {containerName}.");
}
}
// Example usage:
// public static async Task Main(string[] args)
// {
// string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
// string containerName = "mycontainer";
// string blobName = "myblob.txt";
// string filePath = "/path/to/your/local/file.txt";
// await UploadFileAsync(connectionString, containerName, blobName, filePath);
// }
}
It's best practice to store your connection string securely, for example, using Azure Key Vault or application settings.
Once your blobs are stored, you can retrieve them. Here are methods for downloading:
Use the az storage blob download command:
az storage blob download \
--account-name \
--account-key \
--container-name \
--name \
--file
Replace placeholders with your specific details. <BLOB_TO_DOWNLOAD_NAME> is the name of the blob in Azure Storage, and <LOCAL_DESTINATION_PATH> is where you want to save it locally.
Download a blob using the Azure Blob Storage SDK for .NET:
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;
public class BlobDownloader
{
public static async Task DownloadFileAsync(string connectionString, string containerName, string blobName, string filePath)
{
// Create a BlobServiceClient object using the connection string
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
// Get a reference to the container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Get a reference to the blob
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Download the blob to a local file
using (FileStream downloadFileStream = File.Create(filePath))
{
await blobClient.DownloadToAsync(downloadFileStream);
Console.WriteLine($"Successfully downloaded {blobName} to {filePath}.");
}
}
// Example usage:
// public static async Task Main(string[] args)
// {
// string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
// string containerName = "mycontainer";
// string blobName = "myblob.txt";
// string filePath = "/path/to/save/downloaded/file.txt";
// await DownloadFileAsync(connectionString, containerName, blobName, filePath);
// }
}
Before uploading or downloading, you typically need a container. Containers are logical groupings for your blobs.
az storage container create \
--account-name \
--account-key \
--name
using Azure.Storage.Blobs;
using System;
using System.Threading.Tasks;
public class ContainerManager
{
public static async Task CreateContainerAsync(string connectionString, string containerName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
await blobServiceClient.CreateBlobContainerAsync(containerName);
Console.WriteLine($"Container '{containerName}' created successfully.");
}
// Example usage:
// public static async Task Main(string[] args)
// {
// string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
// await CreateContainerAsync(connectionString, "new-documents");
// }
}
Securing your Azure Storage is crucial. Consider the following:
Never embed sensitive keys directly in client-side code (like JavaScript running in a browser).
This section is a demonstration. Actual uploads require a real Azure Storage setup.