【问题标题】:Check if an input blob exists检查输入 blob 是否存在
【发布时间】:2018-03-28 22:44:48
【问题描述】:

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

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

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

对象引用未设置为对象的实例。

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

这是我在 function.json 中的绑定,以防万一 -

{
  "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
}

【问题讨论】:

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


    【解决方案1】:

    工作示例。

    function.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 而不是 C#,所以很遗憾这对我没有帮助:(
    【解决方案2】:

    根据您的描述,我已经使用 PowserShell 使用我的 Http Trigger 检查了这个问题,我可能会遇到与您提到的相同的问题。我也用Node.js检查过,这个问题仍然存在。而对于 C#,我们可以检查 inputBlob 并编写自定义流程。根据我的理解,如果 blob 不存在,PowserShell、Node.js 等的绑定将失败,并且您现在无法捕获异常并自定义您的响应。您可以添加您的问题here 或添加您的反馈here

    【讨论】:

    • 如果没有办法解决这个问题,那就太令人失望了。我在 GitHub 中打开了一个问题,如果 MS 提供任何反馈,我会更新我的问题。谢谢。
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多