If you wan't to read the file as soon as it stored in Blob, you'll need to use BlobTrigger. Here's how you'd setup the function -
[FunctionName("XlsxFunction")]
public static async Task ReadXlsx(
[BlobTrigger("mycontainer/{fileName}.xlsx", Connection = "MyBlobConnection")] Stream file, string fileName,
ILogger log)
{
//install EPPlus nuget
//using OfficeOpenXml;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage(file))
{
var worksheet = package.Workbook.Worksheets[0];
for(int row = 0; row < worksheet.Dimension.End.Row; row++)
{
for(int col = 0; col < worksheet.Dimension.End.Column; col++)
{
//process things;
}
}
//final thing here & save;
}
}
I've never used EPPlus, but this is how you can start reading the file using the library.
Note:
- To avoid reading unnecessary non-excel file, you can see that I've included the .xlsx extension in the BlobTrigger.
- You'll require EPPlus License to use the library for commercial purposes. Addition to this point, please be aware that using the library within (only for internal purposes) a commercial company also requires a commercial license as well. For more info on EPPlus License - https://www.epplussoftware.com/en/LicenseOverview/LicenseFAQ
And you can follow the readme here on different ways to add the license - https://github.com/EPPlusSoftware/EPPlus
Finally,
In the BlobTrigger, I've added the Connection="MyBlobConnection", which means I need to include this key in the local.settings.json (& the application environment configuration after the Function is published to Azure).
This will look something like this -
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MyBlobConnection": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
From the above, note that you can use "AzureWebJobsStorage" directly rather than a custom "MyBlobConnection". This depends on your use case. Your blob connection string can be found in the Azure Storage Service in Azure Portal. For more info - Connection String to an Azure Cloud Storage Account
Additionally, note that I've captured the "fileName" with the BlobTrigger. You can use this to make sure not to run the code if the file name is not what you expected.
e.g. you can add this condition at the start of the function method -
if(!fileName.Contains("accounts")) return;
// continue the processing of data;