使用Azure Functions外部文件绑定时文件损坏 [英] Corrupt file when using Azure Functions External File binding

查看:104
本文介绍了使用Azure Functions外部文件绑定时文件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Azure函数中运行一个非常简单的ExternalFileTrigger方案,就是将一个创建的映像文件从一个onedrive目录复制到另一个目录.

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

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;
}

所有事情似乎都可以正常运行,但是我损坏的新输出图像文件.大小几乎是原来的两倍. 在查看编码时,原始文件位于ANSI中,但是从Azure Functions生成的新文件位于UTF-8中. 当源编码为UTF-8时,当我使用文本文件时,效果很好.

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.

是否可以强制Azure绑定ExternalFileTrigger使用ANSI?或如何解决这个问题?

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

推荐答案

UPDATE 2019:外部文件绑定似乎已从当前版本的Azure Functions中弃用.

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

如果您想按原样复制文件,或者对文件内容执行更多细粒度的二进制操作,我建议将Stream类型而不是string用于输入和输出绑定:

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}");
}

更改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).

这篇关于使用Azure Functions外部文件绑定时文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆