In this article, we’ll explore how to integrate the Azure Storage SDK into a .NET application. We’ll cover the steps to read and write blobs from Azure Storage, and we’ll also discuss how to obtain your Storage Account Key. By the end of this guide, you’ll have a simple command-line application that can interact with Azure Blob Storage.

Step 1: Install Azure Storage SDK

To add the Azure Storage SDK to your .NET application and perform operations such as reading and writing blobs, add a Azure.Storage.Blobs Nuget package reference to the application:

dotnet add package Azure.Storage.Blobs

Step 2: Create Azure Storage Account and Get Account Key

To authenticate your application with Azure Blob Storage, you will need the Storage Account Key. You can find this key in the Azure Portal under the Security + networking -> Access keys section of your Azure Storage Account settings. This key will be used to authenticate with the Azure Storage Account.

Azure Portal showing an Azure Storage Account with the Access keys pane highlighted

Step 3: Add Using Statement

Before the Azure Storage SDK is used, it’s helpful to add a couple using statements to include the Azure.Storage.Blobs namespace within the C# file. This allows for the classes and types within the namespace to be referenced directly without typing out the namespace for each reference.

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

Step 4: Write Code to Connect to Azure Blob Storage

To connect to Azure Blob Storage, you’ll need to instantiate a BlobServiceClient using your Storage Account name and key to authenticate.

First, build the connection string for Azure Blob Storage with the following code. To make this easier to read, the account name and key are set to variables that are then put into a connection string using string interpolation.

string storageAccountName = "your_storage_account_name";
string storageAccountKey = "your_storage_account_key";
string connectionString = $"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net";

Next, the create a new instance of the Azure.Storage.Blobs.BlobServiceClient class, and pass it the connection string containing the Azure Blob Storage Account credentials in its constructor:

var blobServiceClient = new BlobServiceClient(connectionString);

Now that the BlobServiceClient has been instantiated, this class can be used to interact with Azure Blob Storage.

Step 5: Create Blob Container Client

All Blobs in an Azure Storage Account are stored within a Blob Container. To work with the blobs in a container, a BlobContainerClient is needed. Use the following code to create a new BlobContainerClient for a specific container that’s been created within the Azure Storage Account:

string containerName = "<your_container_name>";
var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);

If the Blob Container hasn’t been created, it will need to be create before any blobs can be written to it. The following line of code can be used to create the Blob Container if it doesn’t exist yet:

blobContainerClient.CreateIfNotExists(PublicAccessType.None);

The PublicAccessType for the Blob Container can be set with the following options:

PublicAccessType Option Description
PublicAccessType.Blob Public anonymous read access to blobs within the container is allowed, but container data is not available anonymously.
PublicAccessType.BlobContainer Public anonymous read access to container and blobs is allowed. Clients can enumerate blobs within the container by anonymous request.
PublicAccessType.None No anonymous access to any blob within the container is allowed.

It’s best to always use Azure.Storage.Blobs.Models.PublicAccessType.None unless anonymous access is specifically necessary; for security reasons of course.

Step 6: Create Blob Client

To read or write a blob in the Blob Container, a BlobClient must be created. This can be done using the BlobContainerClient.GetBlobClient passing in the name of the blob:

string blobName = "b59-sample.txt";
var blobClient = containerClient.GetBlobClient(blobName);

This same BlobClient can be used to both read and write the blob to the container.

Step 7: Write/Upload File to Azure Blob Storage

To write / upload a file to blob storage, the BlobClient.UploadAsync method can be called, passing in a Stream to the file contents. The following code reads a local file, using File.OpenRead to create a FileStream, and uploads it to the blob:

FileStream uploadFileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();

Notice, the second parameter to the .UploadAsync method is being passed the value of true. This tells the BlobClient to overwrite the contents of the blob with the new stream.

Step 8: Read/Download File from Azure Blob Storage

To read / download a blob, the BlobClient.DownloadAsync method can be called to download the contents of the blob. Then the returned BlobDownloadInfo will contain information about the blob, like the .Content property allowing access to the contents of the blob. The .CopyToAsync method can then be used to copy the blob contents to another Stream.

BlobDownloadInfo download = await blobClient.DownloadAsync();
using (FileStream downloadFileStream = File.OpenWrite(localFilePath))
{
  await download.Content.CopyToAsync(downloadFileStream);
  downloadFileStream.Close();
}

Conclusion

You’ve now successfully integrated the Azure Storage SDK into your C# .NET application and can read and write blobs to Azure Storage. With these basic operations, you can build upon this foundation to create more complex applications that leverage the power of Azure Blob Storage.

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading