我有一个带有 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/