Retrieving And Storing Documents/ Images Into Azure Blob Storage Using .NET Core Console App And C#

Introduction

 
This is a continuation of my article on using and understanding Azure storage services and exploring Azure blob storage.
 
In this article, we will learn how to upload an image into Azure blob storage account programmatically using C#.
 
We will also download documents from our Azure blob storage into our local drive.
 
Prerequisites
 
Kindly refer to my previous article here to set the foundation of using an Azure blob storage aoocunt for document storage.
In this article, we will cover the following topics 
  • Setting up Azure blob storage and uploading documents using the Azure portal 
  • Accessing documents from Azure blob storage from rhe Azure portal
  • Uploading documents into Azure blob stoarge programmatically using C#
  • Retrieving documents from Azure blob storage programmatically using C#

Implementation

 
Setting up Azure blob storage and uploading documents using the Azure portal 
 
I will be using the same storage account created in a previous article to set up a blob storage container.
 
Azure blob storage requires container instance to upload document.
 
Under your storage account we will create a new container.
 
 
Click on containers and create a new container.
 
Once our container is created we can start uploading documents and images here. 
 
 
My container name is azurebloblearning and I have uploaded a txt file from my local drive.
 
Thus we have created a new blob storage and uploaded a txt document to our blob storage using Azure portal
 
Accessing documents from Azure blob storage from Azure portal:
 
We can click on document in Azure and copy the document url.
 
The document url would look like as below. 
 
https://azurelearn1234.blob.core.windows.net/azurebloblearning/AzureBlobtest.txt
 
The URL first part is the storage account name and indicates it is blob stoarge followed by container name and document name.
 
Copy paste the doument url into new browser ,and you will not able to access the document.
 
Now come back and change the access level as below.
 
 
Now copy paste the document url in browser tab and you will see the document content.
 
Thus we have accessed our document from blob storage using Azure portal.
 
Uploading documents into Azure blob storage programtically using C#.
 
Create a new .NET Core console app in C#.
 
Now install Azure.Storage.Blobs nuget package version 12.6.
 
This is the latest version of the package. It has the desired classes that are used to communicate with Azure blob storage.
 
Import the below namespaces.
  1. using System;  
  2. using Azure.Storage.Blobs;  
  3. using Azure.Storage.Blobs.Models;  
  4. using System.IO;  
  5. using System.Threading.Tasks;  
 Set up static variables.
  1. static string storageconnstring = "access key";  
  2. static string containerName = "azurebloblearning";  
  3. static string filename = "birthdayphoto.jpg";  
  4. static string filepath = "C:\\Nirankari\\birthdayphoto.jpg";  
Under your Azure storage account copy paste any one access connection key and set the connectionstring variable.
 
This is used to access and authenticate Azure blob storage account.
  1. static async Task CreateBlob() {  
  2.     BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);  
  3.     BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
  4.     BlobClient blobClient = containerClient.GetBlobClient(filename);  
  5.     using FileStream uploadFileStream = File.OpenRead(filepath);  
  6.     await blobClient.UploadAsync(uploadFileStream, true);  
  7.     uploadFileStream.Close();  
  8. }  
The bolbserviceclient class acts as handler and accepts connectionstring parameter to connect and authenticate Azure blob storage.
 
The Getblobcontainer client accepts container name parameter.
 
The containerclient object accepts filename and uploadsync method is used to upload the file from our local file path to Azure blob stoarge container.
 
Call the CreateBlob method in our main function.
  1. static void Main(string[] args) {  
  2.     CreateBlob().Wait();  
  3.     Console.WriteLine("Hello World!");  
  4.     Console.ReadKey();  
  5. }  
 We could see image file from our local drive uploaded to Azure blob storage.
 
Thus we have learned how to upload document file from our local using C#.
 
Retreving documents from azure blob storage programatically using C#.
 
Define the download path variable where the blob storage document needs to be saved. 
  1. static string downloadpath = "C:\\Nirankari\\Azure\\birthdayphoto.jpg";  
  2. static async Task GetBlob() {  
  3.         BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);  
  4.         BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
  5.         BlobClient blob = containerClient.GetBlobClient(filename);  
  6.         BlobDownloadInfo blobdata = await blob.DownloadAsync();  
  7.         using(FileStream downloadFileStream = File.OpenWrite(downloadpath)) {  
  8.             await blobdata.Content.CopyToAsync(downloadFileStream);  
  9.             downloadFileStream.Close();  
  10.         }  
The blobserviceclient class acts and handler and accepts connectionstring paramater to connect and authenticate Azure blob storage.
 
The Getblobcontainer client accepts container name parameter.
 
The Getblobclient get the blob document as an object based on file name.
 
The object is dowloaded using download async method.
 
Using file stream the blob object data is copied to local path set in the download path variable.
 
Call the getbolbmethod in our main function.
  1. GetBlob().Wait();  
  2. Console.WriteLine("Hello World!");  
  3. Console.ReadKey();  
We could see the image file downloaded in our local path.
 
Thus we have succesfully retrieved documents from Azure blob storage using C#. 
 

Summary

 
In this article, we have learned how to leverage Azure blob storage for document storage and retrieval .
 
We have also uploaded and downloaded documents from Azure storage account programmatically using C#.
 
Thanks a lot for reading. I hope you liked this article. Please share your valuable suggestions and feedback. Write in the comment box in case you have any questions. Have a good day!


Similar Articles