Perform the following steps:
- Create a new console app named ExcelImport.Client using Visual Studio, as shown in the following screenshot:
- Once the project is created, execute the following commands in the NuGet package manager:
Install-Package Microsoft.Azure.Storage.Blob
Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.FileExtensions
Install-Package Microsoft.Extensions.Configuration.Json
- Add the following namespaces at the top of the Program.cs file:
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;
- The next step is to develop the code in a function named UploadBlob that uploads the Excel file into the blob container that we have created. For the sake of simplicity, the following code uploads the Excel file from a hardcoded location. However, in a typical real-time application, this file would be uploaded by the end user via a web interface. Copy the following code and paste it in the Program.cs file of the ExcelImport.Client application:
private static async Task UploadBlob()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString("StorageConnection"));
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer ExcelBlobContainer = cloudBlobClient.GetContainerReference("Excelimports");
await ExcelBlobContainer.CreateIfNotExistsAsync();
CloudBlockBlob cloudBlockBlob = ExcelBlobContainer.GetBlockBlobReference("EmployeeInformation" + Guid.NewGuid().ToString());
await cloudBlockBlob.UploadFromFileAsync(@"C:\Users\vmadmin\source\repos\POC\ImportExcelPOC\ImportExcelPOC\EmployeeInformation.xlsx");
}
- Now, copy the following code to the Main function. This piece of code just invokes the UploadBlob function, which internally is responsible for uploading the blob:
{
UploadBlob().Wait();
}
catch (Exception ex)
{
Console.WriteLine("An Error has occurred with the message" + ex.Message);
}
- The next step is to create a configuration file named appsettings.json, which contains the storage account's connection string, as shown in the following screenshot:
- Go to the properties of the appsettings.json file and change Copy to Output Directory to the Copy if newer option, so that the properties can by read by the program as shown in the following screenshot:
- Now, build the application and execute it. If you have configured everything, then you should see something as shown in the following screenshot:
- Let's navigate to the storage account and go to the blob container named Excelimports, where you should see the Excel file that we have uploaded, as shown in the following screenshot:
That's it. We have developed an application that is responsible for uploading the blob.