JavaScript:Azure函数blob绑定处理异常 [英] JavaScript: Azure function blob binding handling exceptions
问题描述
我创建了一个azure函数httptrigger,以使用blob输入绑定从blob存储中读取blob.
下面是function.json:
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"name" : "blobContent",
"type": "blob",
"direction": "in",
"path": "containerName/{id}.{extn}",
"connection": "AzureWebJobsStorage"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
]
}
,index.js文件为:
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.id || (req.body && req.body.id)) {
context.res = {
body : {'data' : context.bindings.blobContent},
headers : {'Content-type': 'application/xml"'}
}
}
else {
context.res = {
status: 400,
body: "Please pass a object/chuck id on the query string or in the request body"
};
}
context.done(null,context.res);
};
我同时使用get和post方法来调用httptrigger.由于我使用的是Blob输入绑定,因此在处理index.js之前先检索内容.有了这个,我无法验证API是否以id和extn调用.有没有一种方法可以处理异常并将消息返回给API调用者以传递必要的参数.提前致谢.
因此,函数确实有某种方法,称为问题跟踪此功能在其他情况下的添加./p>
Hi I have created a azure function httptrigger to read a blob from blob storage with blob input bindings.
below is the function.json:
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"name" : "blobContent",
"type": "blob",
"direction": "in",
"path": "containerName/{id}.{extn}",
"connection": "AzureWebJobsStorage"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
]
}
and the index.js file is :
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.id || (req.body && req.body.id)) {
context.res = {
body : {'data' : context.bindings.blobContent},
headers : {'Content-type': 'application/xml"'}
}
}
else {
context.res = {
status: 400,
body: "Please pass a object/chuck id on the query string or in the request body"
};
}
context.done(null,context.res);
};
I am using both get and post method to call the httptrigger. Since i am using blob input binding, the content is retrieved before processing index.js. With this, i couldn't validate whether the API called with id and extn. Is there a way to handle the exception and give a message back to the API caller to pass the necessary parameters. Thanks in advance.
So Functions does have some way of doing this, called Function Filters. This feature allows you to write methods that are called before or after the job function is ran (Invocation Filters), or that are invoked whenever the code encounters an exception in the functions runtime (Exception Filters). You could write an exception filter that catches an exception when the input binding fails and that would accomplish what you want.
Unfortunately, at the time of writing this answer, Function Filters are only compatible with pre-compiled C# functions. There is currently this issue tracking this feature's addition into other scenarios.
这篇关于JavaScript:Azure函数blob绑定处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!