使用Azure Functions在Azure blob中运行exe
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Azure Functions在Azure blob中运行exe相关的知识,希望对你有一定的参考价值。
我想运行一个从C#解决方案生成的exe。这是一个命令行解决方案。我有我的exe存在于Blob存储中。我可以使用azure函数来执行blob中存在的exe。
谢谢。
答案
不,我们不能像这样使用blob存储。您可能需要查看WebJobs以执行此类任务。 https://docs.microsoft.com/en-us/azure/app-service/webjobs-create
另一答案
我们需要检索exe文件然后执行它。以v2 c#http触发器为例。
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
//Download file first
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("StorgeConnectionString");
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("mycontainer");
string fileName = "Console.exe";
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
// Use this path to store file in case the Azure site is read-only
string path = "D:\home\data";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string wholePath = Path.Combine(path, fileName);
await cloudBlockBlob.DownloadToFileAsync(wholePath, FileMode.OpenOrCreate);
// Execute
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = wholePath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
int exitcode = process.ExitCode;
if (exitcode == 0)
{
log.LogInformation($"Executed, output: {output}");
return new OkObjectResult($"Executed, output: {output}");
}
else
{
log.LogError($"Fail to process due to: {error}");
return new ObjectResult($"Fail to process due to: {error}")
{
StatusCode = StatusCodes.Status500InternalServerError
};
}
}
以上是关于使用Azure Functions在Azure blob中运行exe的主要内容,如果未能解决你的问题,请参考以下文章
Azure 函数 - 'azure-functions-host' 文件夹的位置
Azure Functions:如何在 Azure 存储队列的绑定表达式中使用 POCO?
在 Java 中使用 Visual Studio Code 的 Azure Functions 项目
使用Azure Functions在Azure blob中运行exe