5
votes

Edited Thank you @Marco

I'm trying to write a function app that grabs an SVG from a URL and converts it to PNG. I know there are existing API's that do this like CloudConvert, but they don't work nicely with embedded fonts, which is a requirement for me.

Anyway, I wrote a very basic function app that simply downloads a file at this point. Everything works perfectly fine locally, but when I publish to Azure, I get An exception occurred during a WebClient request.

Thanks to @Marco's suggestion, I switched from WebClient to HTTPWebRequest to get more detailed error handling, and as a result, I see the following:

2018-10-11T13:53:53.558 [Info] Function started (Id=e3cbda04-140e-4ef7-ad6c-c871ffe179dd)
2018-10-11T13:53:53.590 [Info] C# HTTP trigger function processed a request. 2018-10-11T13:53:53.752 [Info] Download Fail
2018-10-11T13:53:53.752 [Info] Access to the path
'D:\Windows\system32\734e16961fc276df.svg' is denied.

Am I trying to do something that isn't possible, or is there a fix for this? Is there a way to configure permissions in an Azure function? I need to pull the file down to edit and not just work with the byte array.

Many thanks!

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, 
        TraceWriter log, ExecutionContext context)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string svgURL = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "l", true) == 0)
        .Value;

    if (svgURL == null)
    {
        // Get request body
        dynamic data = await req.Content.ReadAsAsync<object>();
        svgURL = data?.svgURL;
    }
    // download file from URL
    var uniqueName = GenerateId() ;
    try 
    { 
        using (var client = new WebClient())
        {
           client.DownloadFile(svgURL, uniqueName + ".svg" );
        }
    }
    catch (Exception e)
    {
        log.Info("Download Fail");
        log.Info(e.Message);
    }
}
1
Please post your code, not an image of your code. Your question becomes useles, once your image does get deleted. Crawlers and screenreaders also have a hard time interpreting images.Marco
You could also try to use an HttpWebRequest instead of a WebClient to see if you get any more detailed exception messages / Stacktraces.Marco
@Marco got it, thanks, and updated. Didn't know that... will try HttpWebRequest nowMike Baron
And please make sure, that svgUrl is def. not null in production. Because currently it defintily can be.Marco
@Marco got it, thanks. yah it's definitely not. I'm using the same svgUrl testing locally and in prod to be sure.Mike Baron

1 Answers

6
votes

The easiest way to solve this is to use temp storage. I can see why Azure wouldn't want functions cludging up the app directory anyway. Updated code below:

I replaced this:

client.DownloadFile(svgURL, uniqueName + ".svg" );

With this:

client.DownloadFile(svgURL, Path.GetTempPath() + "\\" + uniqueName + ".svg" );

Worked like a charm.

Edit: Below is the GitHub repo where I make this call. There's other stuff going on but you can see where I save to temp storage.
https://github.com/osuhomebase/SVG2PNG-AzureFunction