Azure Blob Storage + .NET Client Library

Azure Blob Storage + .NET Client Library

Azure offers Blob Storage as the cloud storage solution. Blob Storage is a way of storing substantial amounts of unstructured data: data that doesn’t adhere to a specific data model or definition- data that can’t go into rows and columns. For example, video, audio, image, log files, text files, etc. Azure Blob Storage has three types of resources to offer:

  1. The Storage Account
  2. Container(s) within the storage account
  3. Blob(s) within the Container

Prerequisites

  1. Azure Subscription
  2. Azure Storage Account
  3. The current .Net Core SDK for your OS

First, you will need to set up a .NET Core project. Do this with whichever method you please. I typically use Visual Studios, but some prefer doing this directly in their Terminal of choice.

Next, you will need to connect your Storage Account to your .NET Core project. Do this by generating an Access Key inside your Azure Storage Account. Copy the “Connection String” value generated for key1.

Then, set this value as an environment variable named “AZURE_STORAGE_CONNECTION_STRING”. An easy way to set an environment variable without navigating through your system settings is to use your OS Command Prompt. For windows, that would be the CMD with the following syntax:

1
set AZURE_STORAGE_CONNECTION_STRING <yourconnectionstring>

image info

.Net classes to interact with Azure Storage Resources

BlobServiceClient: manipulate Azure Storage resources and blob containers

BlobContainerClient: manipulate Azure Storage containers and their blobs

BlobClient: manipulate Azure Storage Blobs

BlobDownloadInfo: Represent the properties and content returned from downloading a Blob

Tip:

Retrieve the Azure Storage Account’s Connection String inside your .NET

project- From your previously set environment variables.

1
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

Tip:

Add the Azure Blob Storage .Net Client Library with the using statement.

1
2
3
4
5
using Azure.Storage;

using Azure.Storage.Blobs;

using Azure.Storage.Blobs.Models;

Code Examples:

Retrieve the Azure Store Account’s Connection String – Environment Variable:

1
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

List Blobs in a Container:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
BlobContainerClient blobContainerClient = new BlobContainerClient(connectionString, "burpgrid");


Console.WriteLine("Listing blobs...");

var blobnames = new List<string>();

// List all blobs in the container

await foreach (BlobItem blobItem in blobContainerClient.GetBlobsAsync())

{

Console.WriteLine("\t" + blobItem.Name);

blobnames.Add(blobItem.Name);

}

Download Contents from each Blob in a Container to Local:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
foreach (string blobname in blobnames)

{

 

string localFilePath = Path.Combine(localPath, blobname);

// Connect to each blob client

BlobClient blobClient = blobContainerClient.GetBlobClient(blobname);


// Download the blob's contents and save it to a file

BlobDownloadInfo download = await blobClient.DownloadAsync();


using (FileStream downloadFileStream = File.OpenWrite(localFilePath))

{

await download.Content.CopyToAsync(downloadFileStream);

downloadFileStream.Close();

}

}

Clear Local File Path:

1
2
3
4
5
6
7
8
9
//Delete all files in local file path

string localPath = “/.data/”

string[] filePaths = Directory.GetFiles(localPath);

foreach (string filePath in filePaths)

File.Delete(filePath);

Open and Read File Contents to Class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//Open and read files 

foreach (string blobname in blobnames)

{

string localFilePath = Path.Combine(localPath, blobname);

string text = System.IO.File.ReadAllText(localFilePath);

 //Convert to saved JSON

 var scan = JsonConvert.DeserializeObject<ScanResults>(text);

 Console.WriteLine("issue counts: " + scan.issue_counts.total);

 Console.WriteLine("site name: " + scan.site_name);
}

Additional resources:

  1. API reference documentation

  2. Library source code

  3. Package (NuGet)

  4. Samples

  5. Microsoft Documentation

updatedupdated2024-07-312024-07-31