-2

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.

roney
  • 756
  • 1
  • 10
  • 28
  • Please re-read [MCVE] guidance on posting code. "My code fails"/ HTTP status code 500 does not in any way show what fails in your code. And if someone suggest that you simply don't have that file in blob storage you'll likely start expressing ... opinions about posters. So it is far better to provide all necessary information in the post. – Alexei Levenkov Oct 01 '20 at 23:22
  • 1
    How do you expect something like "D:\home\site\wwwroot\tools\GnerateHTML.exe" to work in an Azure function?! And you can't pass a stream as argument to an exe file (`Arguments = $"{myBlob}"`). – Delphi.Boy Oct 02 '20 at 01:37
  • @Delphi.Boy yes, if you go to kudu diagnostic console, you will see the path where the exe is getting copied .and I was able to send text as a parameter to the exe. But I am trying to figure out if I can send a file as input prameter – roney Oct 02 '20 at 02:01
  • Is this a HTTP trigger or a blob trigger, you seem to be trying to do both things? – Liam Oct 02 '20 at 13:22
  • 1
    Azure functions are **serverless**. There is no way it can (or should) try an access an exe on the host machine. You have no idea where or what machine will host it. You have many, many problems with this code – Liam Oct 02 '20 at 13:23
  • I need the function to be executed upon httpTrigger – roney Oct 02 '20 at 13:24
  • @liam, If you can understand what I am trying to do, give me the best way to do this with some pointers to some examples – roney Oct 02 '20 at 13:26
  • How to send a file stored in azure blob storage as input parameter to the Exe? – roney Oct 02 '20 at 13:29
  • 1
    You cannot send anything to any exe! Exe's cannot run inside an azure function they're sever less. So your entire premise is faulty. No one can help because what you are trying to do is impossible. – Liam Oct 02 '20 at 13:41
  • I have updated my code and my question – roney Oct 02 '20 at 13:52
  • @roney, Azure Functions are not a good fit for what you are trying to do. Each time you trigger an Azure function, a new container spins up. What is happening inside this container should be independent of what you do in your function. What is this .exe file doing? Can't you implement what it does directly in your function? Read from blob storage, process it, save the result to blob storage. – Delphi.Boy Oct 02 '20 at 16:35

1 Answers1

0

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.

David Browne - Microsoft
  • 50,175
  • 5
  • 27
  • 51