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
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.
- utilizing System;
- utilizing Azure.Storage.Blobs;
- utilizing Azure.Storage.Blobs.Fashions;
- utilizing System.IO;
- utilizing System.Threading.Duties;
Arrange static variables.
- static string storageconnstring = “entry key”;
- static string containerName = “azurebloblearning”;
- static string filename = “birthdayphoto.jpg”;
- 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.
- static async Process CreateBlob() {
- BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
- BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
- BlobClient blobClient = containerClient.GetBlobClient(filename);
- utilizing FileStream uploadFileStream = File.OpenRead(filepath);
- await blobClient.UploadAsync(uploadFileStream, true);
- uploadFileStream.Shut();
- }
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.
- static void Primary(string[] args) {
- CreateBlob().Wait();
- Console.WriteLine(“Good day World!”);
- Console.ReadKey();
- }
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.
- static string downloadpath = “C:NirankariAzurebirthdayphoto.jpg”;
- static async Process GetBlob() {
- BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
- BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
- BlobClient blob = containerClient.GetBlobClient(filename);
- BlobDownloadInfo blobdata = await blob.DownloadAsync();
- utilizing(FileStream downloadFileStream = File.OpenWrite(downloadpath)) {
- await blobdata.Content material.CopyToAsync(downloadFileStream);
- downloadFileStream.Shut();
- }
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.
- GetBlob().Wait();
- Console.WriteLine(“Good day World!”);
- 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!