使用Azure功能处理文件

时间:2016-07-14 01:03:40

标签: azure azure-functions azure-webjobs

我想处理输入文件并将其输出到某个位置。 FTP或Azure存储。我正在尝试使用Azure功能与SaasFile输入/输出。我收到以下错误:

2016-07-14T00:44:53 Welcome, you are now connected to log-streaming service. 2016-07-14T00:45:00.580 Script for function 'HttpTriggerCSharp1' changed. Reloading. 2016-07-14T00:45:00.580 Compiling function script. 2016-07-14T00:45:00.721 run.csx(24,25): error CS0622: Can only use array initializer expressions to assign to array types. Try using a new expression instead. 2016-07-14T00:45:00.721 Compilation failed.

这是我的功能签名:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string output, TraceWriter log)

绑定:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "apiHubFile",
      "name": "output",
      "path": "path/{file}",
      "connection": "ftp_FTP",
      "direction": "out"
    }
  ],
  "disabled": false
}

我想我在运行签名中遗漏了一些东西。我无法在Azure documentation找到它。

我需要帮助弄清楚如何使用FTP和Azure存储进行处理。谢谢你的帮助。

4 个答案:

答案 0 :(得分:3)

如果你必须使用Http触发器,并且需要根据某些标题值或不需要在函数本身中创建文件名,则使用此示例:

请确保您使用的是函数版本0.4或更高版本(今天发布)

#r "Microsoft.Azure.WebJobs.Extensions.ApiHub"

using System.Net;
using Microsoft.Azure.WebJobs;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, IBinder binder)
{
    //Get request  body  
    string data = await req.Content.ReadAsStringAsync();

    string fileName = "path/" + Guid.NewGuid().ToString() + ".txt";

    var writer = binder.Bind<TextWriter>(new ApiHubFileAttribute("DROPBOX_dropbox", fileName, FileAccess.Write));

    writer.Write(data);
    return req.CreateResponse(HttpStatusCode.OK);  
}

绑定:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

答案 1 :(得分:1)

假设您输出到特定文件名,这是一种可以执行此操作的方法。我在这个例子中绑定了一个Dropbox文件。

using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out string output)
{
    output = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();

    return req.CreateResponse(HttpStatusCode.OK);   
}

bindings:
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "type": "http",
      "name": "res",
      "direction": "out"
    },
    {
      "type": "apiHubFile",
      "name": "output",
      "path": "path/b.txt",
      "connection": "dropbox_DROPBOX",
      "direction": "out",
    }
  ],
  "disabled": false
}

要绑定到不同的文件名,您需要输入或输入触发器并将文件名传递给输出。与所有其他样本相同。

答案 2 :(得分:0)

你真的不需要http触发器。 下面是一个示例,它在dropbox中查看新文件的文件夹(input-cs)并将文件复制到googledrive中的文件夹(output-cs):

using System;

public static void Run(string input, out string output, TraceWriter log)
{
    output = input;
}

绑定:

{

 "bindings": [
    {
      "type": "apiHubFileTrigger",
      "name": "input",
      "direction": "in",
      "path": "input-cs/{name}",
      "connection": "dropbox_DROPBOX"
    },
    {
      "type": "apiHubFile",
      "name": "output",
      "direction": "out",
      "path": "output-cs/{name}",
      "connection": "googledrive_GOOGLEDRIVE"
    }
  ],
  "disabled": false
}

答案 3 :(得分:0)

好消息。外部文件触发器可用于Azure功能。

如果要处理外部FTP文件夹中的文件,请先创建FTP连接,然后再使用它。

所以你在function.json中的FTP连接的绑定数组如下所示。

{
  "bindings": [
    {
      "type": "apiHubFileTrigger",
      "name": "inputFile",
      "direction": "in",
      "path": "input-cs/{name}",
      "connection": "ftp_FTP"
    },
    {
      "type": "apiHubFile",
      "name": "$return",
      "direction": "out",
      "path": "output-cs/{name}",
      "connection": "ftp_FTP"
    }
  ],
  "disabled": false
}

在上面的JSON中,

  

路径

表示要处理的文件的路径。因此,在上述情况下,在 input-cs 文件夹中添加/修改文件时将触发azure函数。

  

连接

表示外部文件连接器名称。