Wednesday, 6 April 2022

Upload And Download File To Azure Blob Storage Using C#

Introduction Azure Storage:

Azure Storage is a service provided by Microsoft to store the data, such as text or binary. You can use this data to make it available to the public or secure it from public access. There are multiple ways I found on the internet to upload the content to Azure Blob Storage, like you can use shared keys, or use a Connection string, or a native app.

Problem

Now, here is the problem. Suppose you have to create a Windows Service which will upload and download your files to the Azure Blob Storage. And as per the requirements, you don’t have to use a shared key or connection string or simply your client didn’t share it. 

The client wants to get it done by Active Directory Authentication. With the Native app, as most of the samples over the internet, the user consent is required if your token expires and that’s why it cannot be used in Windows Service.

Solution

After spending some time on it, I found the right way to upload and download a file on Azure Blob Storage. As I didn’t find any documentation or samples particular to this requirement over the internet, I am sharing it. Hope it will help.

Create an application on Azure

Create an application of Web app/API type on the Azure portal.

 Upload And Download File To Azure Blob Storage Using C#
 

Get Keys for Authentication

You need to have the application id, tenant/Directory Id, and Client Secret key.

After application creation, you will see the below screen where you have the application Id and from the Keys section, you can generate Client Secret key. See the below screens.

 Upload And Download File To Azure Blob Storage Using C#

To get Directory/ Tenant Id.

 Upload And Download File To Azure Blob Storage Using C#

 Upload And Download File To Azure Blob Storage Using C#

Role assignment to your Azure application

Now, go to your storage account that you want to use and assign your application Storage Blob Data Owner/Contributor Role.

 Upload And Download File To Azure Blob Storage Using C#
 
Upload And Download File To Azure Blob Storage Using C#
 
Actually, the above role gives you permissions to read/write/delete on Blob Management and Data. With Management permission, you can read/write/delete a container in the Blob and with Data, you can read/write/delete content in it.
 
Upload And Download File To Azure Blob Storage Using C#
 
Now, you have all the things you need to build an application. Let's see the code snippets.
 
Authentication
 
The first thing you need is to get an access token and you can get it using the below method.
  1. static string GetUserOAuthToken(string tenantId, string applicationId, string clientSecret)  
  2.         {  
  3.             const string ResourceId = "https://storage.azure.com/";  
  4.             const string AuthInstance = "https://login.microsoftonline.com/{0}/";  
  5.   
  6.             string authority = string.Format(CultureInfo.InvariantCulture, AuthInstance, tenantId);  
  7.             AuthenticationContext authContext = new AuthenticationContext(authority);  
  8.   
  9.             var clientCred = new ClientCredential(applicationId, clientSecret);  
  10.             AuthenticationResult result = authContext.AcquireTokenAsync(  
  11.                                                 ResourceId,  
  12.                                                 clientCred  
  13.                                                 ).Result;  
  14.             return result.AccessToken;  
  15.         }     

File Upload and Download Methods

Below is the code snippet to upload and download the file to Azure Blob Storage.
  1. public static class AzureOperations {  
  2.     #region ConfigParams  
  3.     public static string tenantId;  
  4.     public static string applicationId;  
  5.     public static string clientSecret;  
  6.     #endregion  
  7.     public static void UploadFile(AzureOperationHelper azureOperationHelper) {  
  8.         CloudBlobContainer blobContainer = CreateCloudBlobContainer(tenantId, applicationId, clientSecret, azureOperationHelper.storageAccountName, azureOperationHelper.containerName, azureOperationHelper.storageEndPoint);  
  9.         blobContainer.CreateIfNotExists();  
  10.         CloudBlockBlob blob = blobContainer.GetBlockBlobReference(azureOperationHelper.blobName);  
  11.         blob.UploadFromFile(azureOperationHelper.srcPath);  
  12.     }  
  13.     public static void DownloadFile(AzureOperationHelper azureOperationHelper) {  
  14.         CloudBlobContainer blobContainer = CreateCloudBlobContainer(tenantId, applicationId, clientSecret, azureOperationHelper.storageAccountName, azureOperationHelper.containerName, azureOperationHelper.storageEndPoint);  
  15.         CloudBlockBlob blob = blobContainer.GetBlockBlobReference(azureOperationHelper.blobName);  
  16.         blob.DownloadToFile(azureOperationHelper.destinationPath, FileMode.OpenOrCreate);  
  17.     }  
  18.     private static CloudBlobContainer CreateCloudBlobContainer(string tenantId, string applicationId, string clientSecret, string storageAccountName, string containerName, string storageEndPoint) {  
  19.         string accessToken = GetUserOAuthToken(tenantId, applicationId, clientSecret);  
  20.         TokenCredential tokenCredential = new TokenCredential(accessToken);  
  21.         StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);  
  22.         CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, storageAccountName, storageEndPoint, useHttps: true);  
  23.         CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();  
  24.         CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);  
  25.         return blobContainer;  
  26.     }  
  27.     static string GetUserOAuthToken(string tenantId, string applicationId, string clientSecret) {  
  28.         const string ResourceId = "https://storage.azure.com/";  
  29.         const string AuthInstance = "https://login.microsoftonline.com/{0}/";  
  30.         string authority = string.Format(CultureInfo.InvariantCulture, AuthInstance, tenantId);  
  31.         AuthenticationContext authContext = new AuthenticationContext(authority);  
  32.         var clientCred = new ClientCredential(applicationId, clientSecret);  
  33.         AuthenticationResult result = authContext.AcquireTokenAsync(ResourceId, clientCred).Result;  
  34.         return result.AccessToken;  
  35.     }  
  36. }  
Test it in a Console application.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             // Set Ids of your Azure account  
  6.             AzureOperations.applicationId = "";  
  7.             AzureOperations.clientSecret = "";  
  8.             AzureOperations.tenantId = "";  
  9.   
  10.             //Demo Upload File  
  11.             string srcPathToUpload = string.Format(@"C:\Users\abdul.rehman\Desktop\AzureFileUpload\myfile.txt");              
  12.             UploadFile(srcPathToUpload);  
  13.   
  14.   
  15.             //Demo Download File  
  16.             string azurePathInBlob = "dev/files/myfile.txt";  
  17.             string destinationPath= string.Format(@"C:\Users\abdul.rehman\Desktop\AzureFileDownload\myfile.txt");  
  18.             DownloadFile(destinationPath, azurePathInBlob);  
  19.   
  20.         }  
  21.   
  22.         public static void UploadFile(string srcPath)  
  23.         {              
  24.             AzureOperationHelper azureOperationHelper = new AzureOperationHelper();  
  25.             // your Storage Account Name  
  26.             azureOperationHelper.storageAccountName = "dbpoc";  
  27.             azureOperationHelper.storageEndPoint = "core.windows.net";  
  28.             // File path to upload  
  29.             azureOperationHelper.srcPath = srcPath;   
  30.             // Your Container Name   
  31.             azureOperationHelper.containerName = "filecontainer";   
  32.             // Destination Path you can set it file name or if you want to put it in folders do it like below  
  33.             azureOperationHelper.blobName = string.Format("dev/files/" + Path.GetFileName(srcPath));   
  34.             AzureOperations.UploadFile(azureOperationHelper);  
  35.   
  36.         }  
  37.   
  38.         public static void DownloadFile(string destinationPath, string srcPath)  
  39.         {             
  40.             AzureOperationHelper azureOperationHelper = new AzureOperationHelper();  
  41.             // your Storage Account Name  
  42.             azureOperationHelper.storageAccountName = "dbpoc";  
  43.             azureOperationHelper.storageEndPoint = "core.windows.net";  
  44.             // Destination Path where you want to download file  
  45.             azureOperationHelper.destinationPath = destinationPath;  
  46.             // Your Container Name   
  47.             azureOperationHelper.containerName = "filecontainer";  
  48.             // Blob Path in container where to download File  
  49.             azureOperationHelper.blobName = srcPath;  
  50.               
  51.             AzureOperations.DownloadFile(azureOperationHelper);  
  52.   
  53.         }         
  54.     }  

NuGet Dependencies

  1. WindowsAzure.Storage (I have installed v9.3.3).
  2. Microsoft.IdentityModel.Clients.ActiveDirectory (I have installed v4.4.2).
There are a few things you need to check. 
  1. Container name should be in lowercase. I don't know why it is not handled but in this version, it is not working with uppercase.
  2. Permissions are very critical. Your application role must have read/write/delete permissions on Blob Management and Data as well.  

Summary

 
In this above sample, we learned how to manage our files on Azure Blob Storage with Active Directory Authentication using the application Secret Key. And like Native app, no popup will be shown to enter the credentials in order to get the access token.

Monday, 28 March 2022

Export Data In Excel Sheet Using Angular

 In this article, we will learn how we export a data sheet in Excel using Angular. In this demo, we will use Xlsx library to export data in an Excel sheet.

 
Steps required to export data in an Excel:

Step 1

Create an Angular project by using the following command.
 
ng new ExcelImport
 
Export Data In Excel Sheet Using Angular
 
Step 2
 
Open the newly created project in Visual Studio Code and open the terminal. Add the following command to add Xlsx library.
 
npm install xlsx --save 
 
Step 3
 
Add the following command to add Bootstrap in this project
 
npm install --save Bootstrap 
 
Step 4
 
Now open styles.css file and add Bootstrap file reference. We can also add Bootstrap file reference into angular.json file. 

To add reference in styles.css file import this line,

@import '~bootstrap/dist/css/bootstrap.min.css';
 
Step 5
 
Now Open app.component.ts file and import xlsx library
  1. import * as XLSX from 'xlsx';  

Now, add the following lines in app.component.ts file

  1. import { Component, ElementRef, ViewChild } from '@angular/core';  
  2. import * as XLSX from 'xlsx';  
  3. @Component({  
  4.   selector: 'app-root',  
  5.   templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.   @ViewChild('TABLE', { static: false }) TABLE: ElementRef;  
  10.   title = 'Excel';  
  11.   ExportTOExcel() {  
  12.     const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(this.TABLE.nativeElement);  
  13.     const wb: XLSX.WorkBook = XLSX.utils.book_new();  
  14.     XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');  
  15.     XLSX.writeFile(wb, 'ScoreSheet.xlsx');  
  16.   }  
  17.   team: any = [{  
  18.     Sno: 1,  
  19.     Team: 'India',  
  20.     Match: 8,  
  21.     Win: 7,  
  22.     Loss: 0,  
  23.     Cancel: 1,  
  24.     Point: 15  
  25.   
  26.   }, {  
  27.     Sno: 2,  
  28.     Team: 'NewZeland',  
  29.     Match: 8,  
  30.     Win: 6,  
  31.     Loss: 1,  
  32.     Cancel: 1,  
  33.     Point: 13  
  34.   
  35.   },  
  36.   {  
  37.     Sno: '3',  
  38.     Team: 'Aus',  
  39.     Match: 8,  
  40.     Win: 6,  
  41.     Loss: 1,  
  42.     Cancel: 1,  
  43.     Point: 13  
  44.   
  45.   },  
  46.   {  
  47.     Sno: '4',  
  48.     Team: 'England',  
  49.     Match: 8,  
  50.     Win: 5,  
  51.     Loss: 2,  
  52.     Cancel: 1,  
  53.     Point: 11  
  54.   },  
  55.   {  
  56.     Sno: '5',  
  57.     Team: 'S.Africa',  
  58.     Match: 8,  
  59.     Win: 4,  
  60.     Loss: 3,  
  61.     Cancel: 1,  
  62.     Point: 9  
  63.   },  
  64.   {  
  65.     Sno: '6',  
  66.     Team: 'Pak',  
  67.     Match: 8,  
  68.     Win: 4,  
  69.     Loss: 4,  
  70.     Cancel: 1,  
  71.     Point: 9  
  72.   
  73.   },  
  74.   {  
  75.     Sno: '7',  
  76.     Team: 'SriLanka',  
  77.     Match: 8,  
  78.     Win: 3,  
  79.     Loss: 3,  
  80.     Cancel: 2,  
  81.     Point: 8  
  82.   
  83.   },  
  84.   {  
  85.     Sno: '8',  
  86.     Team: 'Bdesh',  
  87.     Match: 8,  
  88.     Win: 2,  
  89.     Loss: 4,  
  90.     Cancel: 2,  
  91.     Point: 6  
  92.   
  93.   }  
  94.   ];  
  95. }  
Now open app.compoenet.html file and add the following html.
  1. <div class="col-lg-12 table-responsive" #TABLE #table>  
  2.   <div style="padding-bottom: 10px;padding-top: 10px;">  
  3.     <div class="row">  
  4.       <div class="col-sm-12">  
  5.         <button (click)="ExportTOExcel()" class="btn btn-success">ExportTOExcel</button> </div>  
  6.     </div>  
  7.   </div>  
  8.   <table class="table table-bordered">  
  9.     <thead class="thead-dark">  
  10.       <tr>  
  11.         <th>S.No</th>  
  12.         <th>Team</th>  
  13.         <th>Match</th>  
  14.         <th>Win</th>  
  15.         <th>Loss</th>  
  16.         <th>Cancel</th>  
  17.         <th>Point</th>  
  18.       </tr>  
  19.     </thead>  
  20.     <tbody>  
  21.       <tr *ngFor="let tm of team">  
  22.         <td>{{tm.Sno}}</td>  
  23.         <td>{{tm.Team}}</td>  
  24.         <td>{{tm.Match}}</td>  
  25.         <td>{{tm.Win}}</td>  
  26.         <td>{{tm.Loss}}</td>  
  27.         <td>{{tm.Cancel}}</td>  
  28.         <td>{{tm.Point}}</td>  
  29.       </tr>  
  30.     </tbody>  
  31.   </table>  
  32. </div>  
Now run the project:
 
Export Data In Excel Sheet Using Angular
 
Click on the Button and check Excel is created
 
Export Data In Excel Sheet Using Angular
 

Summary

 
In this article we learned how we create Export data in an Excel sheet using Angular.

Friday, 18 March 2022

That The Mind Is A Restless Bird; The More It Gets The More It Wants

 We Notice That The Mind Is A Restless Bird; The More It Gets The More It Wants, And Still Remains Unsatisfied. The More We Indulge Our Passions The More Unbridled They Become. Our Ancestors, Therefore, Set A Limit To Our Indulgences. They Saw That Happiness Was Largely A Mental Condition. A Man Is Not Necessarily Happy Because He Is Rich, Or Unhappy Because He Is Poor ... Millions Will Always Remain Poor. ~Author: Mahatma Gandhi




हम देखते हैं कि मन एक बेचैन पक्षी है; जितना अधिक वह चाहता है उतना ही अधिक प्राप्त करता है, और फिर भी असंतुष्ट रहता है। जितना अधिक हम अपने जुनून को पूरा करते हैं, वे उतने ही बेलगाम होते जाते हैं। इसलिए हमारे पूर्वज हमारे कर्मों की सीमा निर्धारित करते हैं। उन्होंने देखा कि खुशी काफी हद तक एक मानसिक स्थिति थी। एक आदमी जरूरी इसलिए खुश नहीं है क्योंकि वह अमीर है, या दुखी है क्योंकि वह गरीब है ... लाखों हमेशा गरीब रहेंगे। ~लेखक: महात्मा गांधी