使用Azure Functions运行Azure Blob中存在的exe

时间:2019-01-17 21:41:10

标签: azure azure-functions azure-storage-blobs

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

谢谢。

2 个答案:

答案 0 :(得分:1)

不,我们不能使用这样的Blob存储。您可能需要研究WebJobs才能完成此类任务。 https://docs.microsoft.com/en-us/azure/app-service/webjobs-create

答案 1 :(得分: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
            };
        }

    }