Upload and Retrieve Blobs
Key Concepts: Azure Blob Storage, SDKs, REST API, .NET, Python, Java, JavaScript, block blobs, append blobs, page blobs.
Uploading and Retrieving Blobs with Azure SDKs
Azure Blob Storage offers several ways to upload and retrieve data. The recommended approach for most applications is to use the Azure Storage client libraries (SDKs) for your preferred programming language. These libraries abstract away the complexities of the REST API, providing a convenient and robust interface for interacting with Blob Storage.
Uploading a Blob (C#)
Use the UploadFromFileAsync method to upload a local file to a blob.
using Azure.Storage.Blobs;
using System;
using System.Threading.Tasks;
public class BlobUploader
{
public static async Task UploadFileAsync(string connectionString, string containerName, string blobName, string filePath)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
using (var stream = System.IO.File.OpenRead(filePath))
{
await blobClient.UploadAsync(stream, true);
Console.WriteLine($"Successfully uploaded {blobName} to container {containerName}.");
}
}
}
Retrieving a Blob (C#)
Use the DownloadToFileAsync method to download a blob to a local file.
using Azure.Storage.Blobs;
using System;
using System.Threading.Tasks;
public class BlobDownloader
{
public static async Task DownloadFileAsync(string connectionString, string containerName, string blobName, string downloadFilePath)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
await blobClient.DownloadToAsync(downloadFilePath);
Console.WriteLine($"Successfully downloaded {blobName} to {downloadFilePath}.");
}
}
Uploading a Blob (Python)
Use the upload_blob method to upload data from a file-like object.
from azure.storage.blob import BlobServiceClient
def upload_blob_from_file(connection_string, container_name, blob_name, file_path):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
with open(file_path, "rb") as data:
container_client.upload_blob(name=blob_name, data=data)
print(f"Successfully uploaded {blob_name} to container {container_name}.")
Retrieving a Blob (Python)
Use the download_blob method to download blob content to a file.
from azure.storage.blob import BlobServiceClient
def download_blob_to_file(connection_string, container_name, blob_name, download_file_path):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(blob_name)
with open(download_file_path, "wb") as download_file:
download_stream = blob_client.download_blob()
download_file.write(download_stream.readall())
print(f"Successfully downloaded {blob_name} to {download_file_path}.")
Uploading a Blob (Node.js)
Use the uploadFile method from the BlobServiceClient.
const { BlobServiceClient } = require("@azure/storage-blob");
async function uploadBlobFromFile(connectionString, containerName, blobName, filePath) {
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadFile(filePath);
console.log(`Successfully uploaded ${blobName} to container ${containerName}.`);
}
Retrieving a Blob (Node.js)
Use the downloadToFile method.
const { BlobServiceClient } = require("@azure/storage-blob");
async function downloadBlobToFile(connectionString, containerName, blobName, downloadFilePath) {
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.downloadToFile(downloadFilePath, 0); // 0 indicates overwrite if exists
console.log(`Successfully downloaded ${blobName} to ${downloadFilePath}.`);
}
Uploading a Blob (Java)
Use the uploadFromFile method of the BlobClient.
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import java.io.IOException;
public class BlobUploaderJava {
public static void uploadFile(String connectionString, String containerName, String blobName, String filePath) throws IOException {
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = containerClient.getBlobClient(blobName);
blobClient.uploadFromFile(filePath);
System.out.println("Successfully uploaded " + blobName + " to container " + containerName + ".");
}
}
Retrieving a Blob (Java)
Use the downloadToFile method.
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import java.io.IOException;
public class BlobDownloaderJava {
public static void downloadFile(String connectionString, String containerName, String blobName, String downloadFilePath) throws IOException {
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = containerClient.getBlobClient(blobName);
blobClient.downloadToFile(downloadFilePath, true); // true indicates overwrite if exists
System.out.println("Successfully downloaded " + blobName + " to " + downloadFilePath + ".");
}
}
Uploading and Retrieving Blobs with REST API
For advanced scenarios or environments where SDKs are not feasible, you can interact directly with Azure Blob Storage using its REST API. This requires constructing HTTP requests and handling responses manually.
Uploading a Blob (REST API)
Use a PUT request to upload a blob. For large files, consider chunked uploads.
Method: PUT
URL: https://{accountName}.blob.core.windows.net/{containerName}/{blobName}
Headers:
x-ms-version: 2020-08-04x-ms-blob-type: BlockBlobContent-Length: {contentLength}Authorization: SharedKey {accountName}:{signature}
Body: The content of the blob.
For authentication, you'll need to generate a Shared Key signature.
Retrieving a Blob (REST API)
Use a GET request to download a blob.
Method: GET
URL: https://{accountName}.blob.core.windows.net/{containerName}/{blobName}
Headers:
x-ms-version: 2020-08-04Authorization: SharedKey {accountName}:{signature}
The response body will contain the blob data.
Choosing the Right Blob Type
Azure Blob Storage supports three types of blobs:
- Block Blobs: Optimized for storing large amounts of unstructured data like documents, images, and videos. Ideal for general-purpose storage.
- Append Blobs: Optimized for append operations, such as writing to log files. Data can only be added to the end of the blob.
- Page Blobs: Optimized for random read/write operations. Used for scenarios like hosting virtual machine disks.