This page provides sample code snippets to demonstrate common operations with Azure Blob Storage. Choose your preferred language to see the relevant examples.
C# Samples
1. Install the SDK
You'll need the Azure.Storage.Blobs NuGet package. Install it using the .NET CLI:
dotnet add package Azure.Storage.Blobs
2. Connect to your Blob Storage Account
Use your connection string to create a BlobServiceClient.
using Azure.Storage.Blobs;
// Replace with your actual connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
3. Upload a Blob
Create a container (if it doesn't exist) and upload a file.
using Azure.Storage.Blobs;
using System.IO;
// Get a client for a specific container
string containerName = "mycontainer";
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync();
// Get a client for a specific blob
string blobName = "my-blob.txt";
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Create a local file to upload
string localFilePath = "path/to/local/file.txt";
using (FileStream uploadFileStream = File.OpenRead(localFilePath))
{
await blobClient.UploadAsync(uploadFileStream, true);
}
Console.WriteLine($"Uploaded blob '{blobName}' to container '{containerName}'.");
4. Download a Blob
Download the content of a blob to a local file.
using Azure.Storage.Blobs;
using System.IO;
string containerName = "mycontainer";
string blobName = "my-blob.txt";
string downloadFilePath = "path/to/downloaded/file.txt";
BlobClient blobClient = blobServiceClient.GetBlobContainerClient(containerName).GetBlobClient(blobName);
using (var downloadFileStream = File.OpenWrite(downloadFilePath))
{
Response download = await blobClient.DownloadAsync();
await download.Value.Content.CopyToAsync(downloadFileStream);
}
Console.WriteLine($"Downloaded blob '{blobName}' to '{downloadFilePath}'.");
5. List Blobs in a Container
Iterate through all blobs in a specified container.
using Azure.Storage.Blobs;
string containerName = "mycontainer";
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Console.WriteLine("Blobs in container:");
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
Console.WriteLine($" - {blobItem.Name}");
}
Python Samples
1. Install the SDK
Use pip to install the Azure Blob Storage client library.
pip install azure-storage-blob
2. Connect to your Blob Storage Account
Use your connection string to create a BlobServiceClient.
from azure.storage.blob import BlobServiceClient
# Replace with your actual connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
3. Upload a Blob
Create a container (if it doesn't exist) and upload a file.
from azure.storage.blob import BlobServiceClient
container_name = "mycontainer"
blob_name = "my-blob.txt"
local_file_name = "path/to/local/file.txt"
# Get a client for a specific container
container_client = blob_service_client.get_container_client(container_name)
container_client.create_container(public_access="Container") # Create if it doesn't exist
# Upload the blob
with open(local_file_name, "rb") as data:
blob_client = container_client.upload_blob(name=blob_name, data=data)
print(f"Uploaded blob '{blob_name}' to container '{container_name}'.")
4. Download a Blob
Download the content of a blob to a local file.
from azure.storage.blob import BlobServiceClient
container_name = "mycontainer"
blob_name = "my-blob.txt"
download_file_path = "path/to/downloaded/file.txt"
blob_client = blob_service_client.get_container_client(container_name).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"Downloaded blob '{blob_name}' to '{download_file_path}'.")
5. List Blobs in a Container
Iterate through all blobs in a specified container.
from azure.storage.blob import BlobServiceClient
container_name = "mycontainer"
container_client = blob_service_client.get_container_client(container_name)
print("Blobs in container:")
blob_list = container_client.list_blobs()
for blob in blob_list:
print(f" - {blob.name}")
JavaScript Samples
1. Install the SDK
Use npm or yarn to install the Azure Blob Storage client library.
npm install @azure/storage-blob
2. Connect to your Blob Storage Account
Use your connection string to create a BlobServiceClient.
import { BlobServiceClient } from "@azure/storage-blob";
// Replace with your actual connection string
const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
3. Upload a Blob
Create a container (if it doesn't exist) and upload a file.
Iterate through all blobs in a specified container.
import { BlobServiceClient } from "@azure/storage-blob";
const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerName = "mycontainer";
async function listBlobs() {
const containerClient = blobServiceClient.getContainerClient(containerName);
console.log("Blobs in container:");
for await (const blobItem of containerClient.listBlobs()) {
console.log(` - ${blobItem.name}`);
}
}
listBlobs().catch(console.error);
Java Samples
1. Add Dependencies
Add the Azure Blob Storage dependency to your pom.xml file.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.27.0</version><!-- Use the latest version -->
</dependency>
2. Connect to your Blob Storage Account
Use your connection string to create a BlobServiceClient.
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
// Replace with your actual connection string
String connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
3. Upload a Blob
Create a container (if it doesn't exist) and upload a file.
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
String containerName = "mycontainer";
String blobName = "my-blob.txt";
String localFilePath = "path/to/local/file.txt";
// Get a client for a specific container
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
containerClient.createIfNotExists();
// Get a client for a specific blob
BlobClient blobClient = containerClient.getBlobClient(blobName);
try {
File file = new File(localFilePath);
FileInputStream fileInputStream = new FileInputStream(file);
blobClient.upload(fileInputStream, file.length());
System.out.println("Uploaded blob '" + blobName + "' to container '" + containerName + "'.");
} catch (FileNotFoundException e) {
e.printStackTrace();
}