Corrupt file when using Azure Functions External File binding

柔情痞子 提交于 2020-05-01 09:03:47

问题


I'm running a very simple ExternalFileTrigger scenario in Azure Functions were I copy one created image file from one onedrive directory to another.

function.json

    {
  "bindings": [
    {
      "type": "apiHubFileTrigger",
      "name": "input",
      "direction": "in",
      "path": "Bilder/Org/{name}",
      "connection": "onedrive_ONEDRIVE"
    },
    {
      "type": "apiHubFile",
      "name": "$return",
      "direction": "out",
      "path": "Bilder/Minimized/{name}",
      "connection": "onedrive_ONEDRIVE"
    }
  ],
  "disabled": false
}

run.csx

using System;

public static string Run(string input, string name, TraceWriter log)
{
    log.Info($"C# File trigger function processed: {name}");
    return input;
}

Every things seems to work well BUT the new output image file i corrupt. The size is almost twice as big. When looking at the encoding the original file is in ANSI but the new generated file from Azure Functions is in UTF-8. It's working fine when I'm using a text file when source encoding is UTF-8.

Is it possible to force Azure binding ExternalFileTrigger to use ANSI? Or how to solve this?


回答1:


UPDATE 2019: The external file binding seems to be deprecated from the current version of Azure Functions.


If you want to copy the file as-is, or do more fine-grained binary operations on the file contents, I recommend using Stream type instead of string for your input and output bindings:

public static async Task Run(Stream input, Stream output, string name, TraceWriter log)
{
    using (MemoryStream ms = new MemoryStream())
    {
        input.CopyTo(ms);
        var byteArray = ms.ToArray();
        await output.WriteAsync(byteArray, 0, byteArray.Length);
    }
    log.Info($"C# File trigger function processed: {name}");
}

Change the ouput binding in function.json:

  "name": "output",

This function will do exact binary copy of the file, without conversion.

You can see which other types you can use for bindings in External File bindings (see "usage" sections).



来源:https://stackoverflow.com/questions/44734460/corrupt-file-when-using-azure-functions-external-file-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!