Azure

Retrieving And Storing Paperwork/ Photographs Into Azure Blob Storage Utilizing .NET Core Console App And C#

Introduction

 

This can be a continuation of my article on utilizing and understanding Azure storage providers and exploring Azure blob storage.

 

On this article, we’ll discover ways to add a picture into Azure blob storage account programmatically utilizing C#.

 

We can even obtain paperwork from our Azure blob storage into our native drive.

 

Conditions

 

Kindly confer with my earlier article right here to set the muse of utilizing an Azure blob storage aoocunt for doc storage.

On this article, we’ll cowl the next subjects 

  • Establishing Azure blob storage and importing paperwork utilizing the Azure portal 
  • Accessing paperwork from Azure blob storage from rhe Azure portal
  • Importing paperwork into Azure blob stoarge programmatically utilizing C#
  • Retrieving paperwork from Azure blob storage programmatically utilizing C#

Implementation

 

Establishing Azure blob storage and importing paperwork utilizing the Azure portal 

 

I will probably be utilizing the identical storage account created in a earlier article to arrange a blob storage container.

 

Azure blob storage requires container occasion to add doc.

 

Below your storage account we’ll create a brand new container.

 

 

Click on on containers and create a brand new container.

 

As soon as our container is created we will begin importing paperwork and pictures right here. 

 

 

My container title is azurebloblearning and I’ve uploaded a txt file from my native drive.

 

Thus we’ve got created a brand new blob storage and uploaded a txt doc to our blob storage utilizing Azure portal

 

Accessing paperwork from Azure blob storage from Azure portal:

 

We are able to click on on doc in Azure and replica the doc url.

 

The doc url would appear like as under. 

 

https://azurelearn1234.blob.core.home windows.internet/azurebloblearning/AzureBlobtest.txt

 

The URL first half is the storage account title and signifies it’s blob stoarge adopted by container title and doc title.

 

Copy paste the doument url into new browser ,and you’ll not in a position to entry the doc.

 

Now come again and alter the entry degree as under.

 

 

Now copy paste the doc url in browser tab and you will note the doc content material.

 

Thus we’ve got accessed our doc from blob storage utilizing Azure portal.

 

Importing paperwork into Azure blob storage programtically utilizing C#.

 

Create a brand new .NET Core console app in C#.

 

Now set up Azure.Storage.Blobs nuget package deal model 12.6.

 

That is the newest model of the package deal. It has the specified courses which can be used to speak with Azure blob storage.

 

Import the under namespaces.

  1. utilizing System;  
  2. utilizing Azure.Storage.Blobs;  
  3. utilizing Azure.Storage.Blobs.Fashions;  
  4. utilizing System.IO;  
  5. utilizing System.Threading.Duties;  

 Arrange static variables.

  1. static string storageconnstring = “entry key”;  
  2. static string containerName = “azurebloblearning”;  
  3. static string filename = “birthdayphoto.jpg”;  
  4. static string filepath = “C:Nirankaribirthdayphoto.jpg”;  

Below your Azure storage account copy paste anybody entry connection key and set the connectionstring variable.

 

That is used to entry and authenticate Azure blob storage account.

  1. static async Process CreateBlob() {  
  2.     BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);  
  3.     BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
  4.     BlobClient blobClient = containerClient.GetBlobClient(filename);  
  5.     utilizing FileStream uploadFileStream = File.OpenRead(filepath);  
  6.     await blobClient.UploadAsync(uploadFileStream, true);  
  7.     uploadFileStream.Shut();  
  8. }  

The bolbserviceclient class acts as handler and accepts connectionstring parameter to attach and authenticate Azure blob storage.

 

The Getblobcontainer shopper accepts container title parameter.

 

The containerclient object accepts filename and uploadsync methodology is used to add the file from our native file path to Azure blob stoarge container.

 

Name the CreateBlob methodology in our foremost perform.

  1. static void Primary(string[] args) {  
  2.     CreateBlob().Wait();  
  3.     Console.WriteLine(“Good day World!”);  
  4.     Console.ReadKey();  
  5. }  

 We might see picture file from our native drive uploaded to Azure blob storage.

 

Thus we’ve got discovered the right way to add doc file from our native utilizing C#.

 

Retreving paperwork from azure blob storage programatically utilizing C#.

 

Outline the obtain path variable the place the blob storage doc must be saved. 

  1. static string downloadpath = “C:NirankariAzurebirthdayphoto.jpg”;  
  2. static async Process 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.         utilizing(FileStream downloadFileStream = File.OpenWrite(downloadpath)) {  
  8.             await blobdata.Content material.CopyToAsync(downloadFileStream);  
  9.             downloadFileStream.Shut();  
  10.         }  

The blobserviceclient class acts and handler and accepts connectionstring paramater to attach and authenticate Azure blob storage.

 

The Getblobcontainer shopper accepts container title parameter.

 

The Getblobclient get the blob doc as an object primarily based on file title.

 

The thing is dowloaded utilizing obtain async methodology.

 

Utilizing file stream the blob object knowledge is copied to native path set within the obtain path variable.

 

Name the getbolbmethod in our foremost perform.

  1. GetBlob().Wait();  
  2. Console.WriteLine(“Good day World!”);  
  3. Console.ReadKey();  

We might see the picture file downloaded in our native path.

 

Thus we’ve got succesfully retrieved paperwork from Azure blob storage utilizing C#. 

 

Abstract

 

On this article, we’ve got discovered the right way to leverage Azure blob storage for doc storage and retrieval .

 

Now we have additionally uploaded and downloaded paperwork from Azure storage account programmatically utilizing C#.

 

Thanks so much for studying. I hope you favored this text. Please share your useful ideas and suggestions. Write within the remark field in case you will have any questions. Have a very good day!

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close