[英]Azure function execute exe by getting file from azure blob storage as input params
這是我粘貼在下面的示例代碼,一個運行 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 中。
如何將 azure 中的文件作為輸入參數發送到 azure 函數?
如前所述,你不能。 可以發送存儲在 Azure Blob 存儲中的文件的文件名,然后讓 Azure 函數或 .EXE 讀取該文件。 例如
https://mystorageaccount.blob.core.windows.net/mycontainer/whatever/foo.txt
如果僅使用文件名,則 Azure 函數將需要進行身份驗證才能檢索 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 請求正文的一部分發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.