powershell - 检查输入 blob 是否存在

标签 powershell azure binding azure-blob-storage azure-functions

我有一个带有 Blob Store 输入的 Azure Function。我可以使用 $inputFile 变量访问输入文件,这非常简单。

为了允许动态 blob 选择,我传递了一个 config 查询参数,其中包含要选择的配置的名称。

我遇到的唯一问题是,如果有人传递了不存在的 blob 的名称,Azure Function 会立即返回 500 错误,这对用户来说不是特别友好 -

Object reference not set to an instance of an object.

看起来好像这个错误是在我的脚本执行之前生成的,所以它可能是不可能的,但是有什么方法可以改变这种行为,以便我可以向用户发送更有用的消息。

这是我来自 function.json 的绑定(bind),以防万一 -

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "function",
      "methods": [
        "get"
      ]
    },
    {
      "type": "blob",
      "name": "inputBlob",
      "path": "configs/{config}.json",
      "connection": "AzureWebJobsDashboard",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

最佳答案

工作示例。

函数.json:

{
  "bindings": [
    {
      "name": "info",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "function"
    },
    {
      "name": "inputBlob",
      "type": "blob",
      "direction": "in",
      "path": "configs/{config}.json",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}

运行.csx:

using System.Net;

public class BlobInfo
{
    public string config { get; set; }
}

public static HttpResponseMessage Run(HttpRequestMessage req, BlobInfo info, string inputBlob)
{
    if (inputBlob == null) {
        return req.CreateResponse(HttpStatusCode.NotFound);
    } 

    return req.CreateResponse(HttpStatusCode.OK, new {
        data = $"{inputBlob}"
    });
}

关于powershell - 检查输入 blob 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46786800/

相关文章:

带有强制参数的 Powershell Cmdlet

json - 逻辑应用 - Http+Swagger - 基于 token 的身份验证的 header 属性

c# - 我可以在 Azure Functions 2.0 中同时使用 dotnet 和 node 吗?

objective-c - 具有 subview 和多个 NIB 的核心数据绑定(bind)

powershell - 在目录中运行所有 PowerShell 脚本

powershell - 使TeamCity识别Powershell消息

rest - 在PowerShell中访问枚举名称

Azure.Search.Documents - 使用集合更新或插入对象

mvvm - 更改ObservableCollection的属性不起作用

javascript - 将数组绑定(bind)到 Knockout 样式绑定(bind)