【问题标题】:Azure function execute exe by getting file from azure blob storage as input paramsAzure 函数通过从 azure blob 存储中获取文件作为输入参数来执行 exe
【发布时间】:2021-01-17 16:51:06
【问题描述】:

这是我粘贴在下面的示例代码,一个运行 exe 并接受字符串作为输入参数的 HttpTriggered azure 函数。并返回输出。我在下面发布的代码工作得很好。 我正在尝试从这里扩展我的代码以接受文件作为输入参数而不是字符串输入。

我的问题:如何将 azure blob 存储中的文件/文件内容(大小>10mb)作为输入参数发送到 azure 函数?

有没有更好的方法来处理这种情况?

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}");
        }
    }
}

现在我想修改上面的代码,从 azure blob 存储中发送一个文件作为输入参数而不是字符串。

当 Httptrigged 的​​这个 azurefunction 时,我的代码应该从 azure blobstorage 中获取一个文件作为 exe 的输入参数,并生成一个 htmlfile 并存储到 azureblobstorage 中。

【问题讨论】:

  • 请重新阅读minimal reproducible example 发布代码指南。 “我的代码失败”/HTTP 状态代码 500 不会以任何方式显示您的代码中出现的问题。如果有人建议您在 blob 存储中根本没有该文件,您可能会开始表达......关于海报的意见。所以最好在帖子中提供所有必要的信息。
  • 你如何期望像“D:\home\site\wwwroot\tools\GnerateHTML.exe”这样的东西在 Azure 函数中工作?!而且您不能将流作为参数传递给 exe 文件 (Arguments = $"{myBlob}")。
  • @Delphi.Boy 是的,如果你去 kudu 诊断控制台,你会看到 exe 被复制的路径。我能够将文本作为参数发送给 exe。但我想知道是否可以将文件作为输入参数发送
  • Azure 函数是无服务器。它无法(或应该)尝试访问主机上的 exe。您不知道将在何处或哪台机器上托管它。这段代码有很多很多问题
  • 您不能向任何 exe 发送任何内容! Exe 不能在一个 azure 函数中运行,它们的服务器更少。所以你的整个前提是错误的。没有人可以提供帮助,因为您尝试做的事情是不可能的。

标签: c# .net-core azure-blob-storage azure-function-app


【解决方案1】:

如何将来自 azure 的文件作为输入参数发送到 azure 函数?

如前所述,您不能。你可以发送存储在 Azure Blob 存储中的文件的文件名,然后让你的 Azure 函数或 .EXE 读取该文件。例如

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

如果您只使用文件名,Azure 函数将需要 authenticate 来检索 blob。

或者您可以使用 SAS 令牌生成文件名

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

或者调用代码可以下载文件并将内容作为 HTTP 请求正文的一部分发布。

【讨论】:

    猜你喜欢
    • 2023-01-16
    • 2018-08-12
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2022-07-21
    • 2019-11-16
    相关资源
    最近更新 更多