简体   繁体   English

Azure函数-拒绝访问路径

[英]Azure Functions - Access To Path Is Denied

I am getting the following error when running the published version of my function, it should be noted that when I run this locally it works correctly. 运行函数的发布版本时出现以下错误,应该注意的是,在本地运行此函数时,它可以正常运行。

Access to the path 'D:\\Program Files (x86)\\SiteExtensions\\Functions\\1.0.12205\\wkhtmltopdf' is denied. 拒绝访问路径“ D:\\ Program Files(x86)\\ SiteExtensions \\ Functions \\ 1.0.12205 \\ wkhtmltopdf”。 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost) at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost) at System.IO.Directory.CreateDirectory(String path) at NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs() at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml, Stream output) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml) at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent) at HTMLtoPDF.Function1.d__0.MoveNext() 在System.IO.Directory.InternalCreateDirectoryHelper(字符串路径,布尔值CheckHost)在System.IO.Directory.InternalCreateDirectory(字符串fullPath,字符串路径,对象dirSecurityObj,布尔值CheckHost)的System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath) )的NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()的System.IO.Directory.CreateDirectory(String path),位于NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput []的HTMLFiles,String Inputf NReco.PdfGenerator.HtmlToPd()上的NReco.PdfGenerator.HtmlToPdf() .MoveNext()

My application basically transforms HTML into a PDF file, uploads to azure and returns the result uri. 我的应用程序基本上将HTML转换为PDF文件,然后上传到azure并返回结果uri。

As you can see in my code below I don't make any references to this path. 如您在下面的代码中看到的,我没有对该路径进行任何引用。

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();

        }
    }
}

Looks like NReco PdfConverter uses wkhtmltopdf.exe under the hood to generate the PDF and when calling NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs() it is getting the access denied exception. 看起来NReco PdfConverter在后台使用wkhtmltopdf.exe生成PDF,并且在调用NReco.PdfGenerator.HtmlToPdfConverter.EnsureWkHtmlLibs()它获取了拒绝访问异常。

See here for possible resolution: https://github.com/nreco/nreco/issues/4 请参阅此处以获取可能的解决方案: https : //github.com/nreco/nreco/issues/4

The problem is in below two lines. 问题出在下面两行。 It is trying to create folders on file system. 它正在尝试在文件系统上创建文件夹。 When you host your program in Azure, folder permitted 在Azure中托管程序时,允许使用文件夹

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

Can you use the stream variant of GeneratePdf which dos not generate any intermittent folder. 您可以使用不生成任何间歇性文件夹的GeneratePdf的流变体吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM