使用Azure Functions在Azure blob中运行exe

问题描述 投票:0回答:2

我想运行一个从C#解决方案生成的exe。这是一个命令行解决方案。我有我的exe存在于Blob存储中。我可以使用azure函数来执行blob中存在的exe。

谢谢。

azure azure-functions azure-storage-blobs
2个回答
1
投票

不,我们不能像这样使用blob存储。您可能需要查看WebJobs以执行此类任务。 https://docs.microsoft.com/en-us/azure/app-service/webjobs-create


1
投票

我们需要检索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
            };
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.