Azure Function VS2017预览工具 - 如何绑定到CloudBlockBlob?

时间:2017-05-29 14:43:04

标签: c# azure visual-studio-2017 azure-functions

注意:我已经解决了这个问题,但无论如何我都在这里发帖,所以其他人希望能比我更快地找到答案: - )

我有一个Azure Functions"类库"我在VS2015中创建的项目,现在我正在尝试将该项目迁移到新的" Azure Functions" VS2017中的项目类型15.3预览(使用用于Azure功能的Visual Studio 2017工具)。

在VS2015中,C#函数签名如下所示:

public static HttpResponseMessage Run(HttpRequestMessage req, CloudBlockBlob myFile)

我的function.json看起来像这样:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "authLevel": "anonymous",
      "route": "files/{id}/download"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "myFile",
      "type": "blob",
      "direction": "inout",
      "path": "mycontainer/{id}",
      "connection": "[my connection string]"
    }
  ],
  "disabled": false
}

在VS2017中,我必须使用绑定属性而不是function.json,所以我尝试了这个:

public static HttpResponseMessage Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "files/{id}/download")] HttpRequestMessage req,
    [Blob("mycontainer/{id}", FileAccess.Read)] CloudBlockBlob myFile)

但是,当我尝试运行时,我收到此错误消息:

[Function Name]: Microsoft.Azure.WebJobs.Host: Error indexing method '[Function Name]'. Microsoft.Azure.WebJobs.Host: Cannot bind blob to CloudBlockBlob using access Read.

1 个答案:

答案 0 :(得分:0)

诀窍是,即使我的代码只需要读取(FileAccess.Read)blob的属性,我需要在绑定属性中指定FileAccess.ReadWrite,如下所示:

public static HttpResponseMessage Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "files/{id}/download")] HttpRequestMessage req,
    [Blob("mycontainer/{id}", FileAccess.ReadWrite)] CloudBlockBlob myFile)

我应该记得(这是我必须将绑定方向设置为" inout"在我的function.json中)... d'哦。据我所知,FileAccess.ReadWrite是绑定ICloudBlockBlob,CloudBlockBlob或CloudPageBlob值的唯一有效FileAccess值。