问题
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