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.
- using System;
- using Azure.Storage.Blobs;
- using Azure.Storage.Blobs.Models;
- using System.IO;
- using System.Threading.Tasks;
Set up static variables.
- static string storageconnstring = "access key";
- static string containerName = "azurebloblearning";
- static string filename = "birthdayphoto.jpg";
- 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.
- static async Task CreateBlob() {
- BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
- BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
- BlobClient blobClient = containerClient.GetBlobClient(filename);
- using FileStream uploadFileStream = File.OpenRead(filepath);
- await blobClient.UploadAsync(uploadFileStream, true);
- uploadFileStream.Close();
- }
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.
- static void Main(string[] args) {
- CreateBlob().Wait();
- Console.WriteLine("Hello World!");
- Console.ReadKey();
- }
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.
- static string downloadpath = "C:\\Nirankari\\Azure\\birthdayphoto.jpg";
- static async Task GetBlob() {
- BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
- BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
- BlobClient blob = containerClient.GetBlobClient(filename);
- BlobDownloadInfo blobdata = await blob.DownloadAsync();
- using(FileStream downloadFileStream = File.OpenWrite(downloadpath)) {
- await blobdata.Content.CopyToAsync(downloadFileStream);
- downloadFileStream.Close();
- }
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.
- GetBlob().Wait();
- Console.WriteLine("Hello World!");
- 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!