Azure function execute exe by getting file from azure blob storage as input params

500 views Asked by At

Here is my sample code pasted below, an HttpTriggered azure function that runs an exe and accepts a string as input parameter. and returns output.The code I posted below works perfectly fine. I am trying to expand my code from here to accept file as input parameter instead of string input.

My Question: How to send a file/file contents(size>10mb) from azure blob storage as an input parameter to the azure function?

Is there a better way to handle such scenario?

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace execFuncApp
{
    public static class ExecFunc
    {
        [FunctionName("ExecFunc")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext executionContext)
        {
            // you may have better ways to do this, for demonstration purpose i have chosen to keep things simple and declare varables locally. 
            string WorkingDirectoryInfo = @"D:\home\site\wwwroot\ExecFunc";
            string ExeLocation = @"D:\home\site\wwwroot\files\ConsoleApp1.exe";

            var msg = "";       //Final Message that is passed to function 
            var output = "";    //Intercepted output, Could be anything - String in this case. 
            
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
          
            name = name ?? data?.name;

          
            try
            {
                

                // Values that needs to be set before starting the process. 
                ProcessStartInfo info = new ProcessStartInfo
                {
                    WorkingDirectory = WorkingDirectoryInfo,
                    FileName = ExeLocation,
                    Arguments = name,
                    WindowStyle = ProcessWindowStyle.Minimized,
                    UseShellExecute = false,
                    CreateNoWindow = true
                };

                Process proc = new Process
                {
                    StartInfo = info
                };

                //  Discard any information about the associated process that has been cached inside the process component.
                proc.Refresh();

                // for the textual output of an application to written to the System.Diagnostics.Process.StandardOutput stream. 
                proc.StartInfo.RedirectStandardOutput = true;

                // Starts the Process, with the above configured values. 
                proc.Start();

                // Scanning the entire stream and reading the output of application process and writing it to a local variable. 
                while (!proc.StandardOutput.EndOfStream)
                {
                    output = proc.StandardOutput.ReadLine();
                }

               
                msg = $"consoleApp1.exe {DateTime.Now} :  Output: {output}";
            }
            catch (Exception e)
            {
                msg = $"consoleApp1.exe {DateTime.Now} : Error Somewhere! Output: {e.Message}";
            }

            //Logging Output, you can be more creative than me.
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            log.LogInformation($"{msg}");

            return (ActionResult)new OkObjectResult($"{msg}");
        }
    }
}

Now I want to modify my code above to send a file from azure blob storage as input parameter instead of a string.

When Httptrigged's this azurefunction, my code should get a file from azure blobstorage as input parameter to the exe and generate an htmlfile and store into the azureblobstorage.

1

There are 1 answers

0
David Browne - Microsoft On

How to send a file from azure as an input parameter to the azure function?

As stated, you can't. You can send a filename of a file stored in Azure Blob Storage, and then have your Azure Function, or your .EXE read that file. eg

https://mystorageaccount.blob.core.windows.net/mycontainer/whatever/foo.txt

If you use just the filename, the Azure Function will need to authenticate to retrieve the blob.

Or you could generate the filename with a SAS token

https://mystorageaccount.blob.core.windows.net/whatever/foo.txt?sv=2019-10-10&st=2020-10-02T14%3A38%3A03Z&se=2021-10-03T14%3A38%3A00Z&sr=b&sp=r&sig=SShOhAQH2mjMrgTZQeGIl0T1jWGRX2x6kwO%2AI0U4fQQ%3D

Or the calling code could download the file and POST the contents as part of the HTTP request body.