Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading files to Azure function app to manipulate

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);
    }
}
like image 694
Mike Baron Avatar asked Oct 11 '18 13:10

Mike Baron


People also ask

How do I upload files to Azure function app?

Create a HTTP trigger azure function using C# to upload files to blob storage. So open Visual Studio and Go to File -> New -> Project. Search "Azure Functions" in the search box and select the Azure function template and click on Next. Give a name to the function project and click on Create.

How do I edit my Azure function app?

The Functions editor built into the Azure portal lets you update your function code and configuration (function. json) files directly in the portal. Select your function app, then under Functions select Functions. Choose your function and select Code + test under Developer.


1 Answers

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

like image 73
Mike Baron Avatar answered Sep 22 '22 10:09

Mike Baron