Azure函数-拒绝访问路径

时间:2019-01-08 14:13:50

标签: c# azure azure-functions

在运行函数的已发布版本时出现以下错误,应该注意的是,当我在本地运行此函数时,它可以正常工作。

  

访问路径'D:\ Program Files   (x86)\ SiteExtensions \ Functions \ 1.0.12205 \ wkhtmltopdf'被拒绝。在   System.IO .__ Error.WinIOError(Int32 errorCode,字符串也许全路径)位于   System.IO.Directory.InternalCreateDirectory(String fullPath,String   路径,对象dirSecurityObj,布尔值checkHost)位于   System.IO.Directory.InternalCreateDirectoryHelper(字符串路径,布尔值   checkHost)位于System.IO.Directory.CreateDirectory(字符串路径),位于   NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()在   NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput []   htmlFiles,字符串inputContent,字符串coverHtml,字符串   outputPdfFilePath,Stream outputStream)位于   NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent,   字符串coverHtml,流输出)在   NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent,   字符串CoverHtml)在   NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent)   在HTMLtoPDF.Function1.d__0.MoveNext()

我的应用程序基本上将HTML转换为PDF文件,然后上传到azure并返回结果uri。

正如您在下面的代码中看到的那样,我对此路径没有任何引用。

using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using NReco.PdfGenerator;
using System;

namespace HTMLtoPDF
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            string pdfurl = "";
            // parse query parameter
            string html = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "html", true) == 0)
                .Value;

            if (html == null)
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync<object>();
                html = data?.html;
            }
            try
            {
                HtmlToPdfConverter converter = new HtmlToPdfConverter();
                var genpdf = converter.GeneratePdf(html);
                var stream = new MemoryStream(genpdf, writable: false);
                 pdfurl = await UploadFileAsBlobAsync(stream, Guid.NewGuid()+".pdf");
            }
            catch (Exception ex)
            {
             pdfurl = ex.Message+Environment.NewLine+ex.InnerException;
            }

            return html == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass html on the query string or in the request body")
                : req.CreateResponse(HttpStatusCode.OK, pdfurl);
        }
        public static async Task<string> UploadFileAsBlobAsync(Stream stream, string filehtml)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(HIDDENFORSECURITY);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("applicationimages");

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(filehtml);

           await  blockBlob.UploadFromStreamAsync(stream);

            stream.Dispose();
            return blockBlob?.Uri.ToString();

        }
    }
}

2 个答案:

答案 0 :(得分:1)

看起来像NReco PdfConverter在幕后使用wkhtmltopdf.exe生成PDF,并且在调用NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()时遇到拒绝访问异常。

有关可能的解决方法,请参见此处: https://github.com/nreco/nreco/issues/4

答案 1 :(得分:0)

问题在下面两行中。它正在尝试在文件系统上创建文件夹。 在Azure中托管程序时,允许文件夹

HtmlToPdfConverter converter = new HtmlToPdfConverter();
var genpdf = converter.GeneratePdf(html);

您可以使用GeneratePdf的流变体吗,它不生成任何间歇性文件夹。