【发布时间】:2017-07-27 02:17:35
【问题描述】:
我创建了一个接收 HTTP 请求并返回 HTTP 请求的 Azure 函数。功能:
- 接受 HTTP 请求
- 使用在 n 分钟/小时后过期的共享访问签名创建指向 Blob 存储中某个位置的 URI
- 返回 302 状态代码,并将位置标头设置为将在 n 分钟/小时后过期的 URI
当我将 blob 的路径放在查询参数中时,我能够让它工作,但是当该变量在路由模板中时它会失败。
我尝试制作路由模板:storage/{containerName:alpha}/{path:alpha},但它总是返回 404。下面是如何构造请求的示例 cURL。
GET /api/storage/example-container-name/example.jpg?code=SSBhbSBhIHRlYXBvdCwgZG8geW91IHRoaW5rIEkgd291bGQgcHV0IGEgcGFzc3dvcmQgaGVyZT8= HTTP/1.1
Host: hostdoesnotexist.azurewebsites.net
Cache-Control: no-cache
**注意:主机不是真实的,路径和代码不是真实的。*
我确实发现这个问题与 Azure Functions Proxy 做同样的事情有关,但这个问题不适用于 Functions。
Azure Functions Proxy - route to storage account
使用这个Azure Functions HTTP and webhook bindings 示例,并滚动到自定义HTTP 端点 部分,我使用以下代码创建了另一个函数:
Function.json - id 从 int 改变?到阿尔法
{
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get"
],
"route": "products/{category:alpha}/{id:alpha}",
"authLevel": "function"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
运行.csx
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
string category,
string id,
TraceWriter log)
{
if (id == null)
return req.CreateResponse(HttpStatusCode.OK, $"All {category} items were requested.");
else
return req.CreateResponse(HttpStatusCode.OK, $"{category} item with id = {id} has been requested.");
}
所以如果路由是 products/test/abcd 那么它会响应:
200 - “已请求 id = abc 的测试项目。”
但是,如果您将其更改为 products/test/abcd.jpg,那么它会响应:
404 - 您要查找的资源已被删除、名称已更改或暂时不可用。
这与我在创建的其他函数中看到的行为相同。
有谁知道这是否是代理示例中的错误,还是我的路线看起来不同?同样,我使用查询参数进行了这项工作,但是当我将变量移动到路由模板时它失败了。
已编辑 - 根据反馈添加文件 函数.json
{
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get"
],
"route": "products/{category:alpha}",
"authLevel": "function"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
运行.csx
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
string category,
TraceWriter log)
{
string id = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0)
.Value;
if (id == null)
return req.CreateResponse(HttpStatusCode.OK, $"All {category} items were requested.");
else
return req.CreateResponse(HttpStatusCode.OK, $"{category} item with id = {id} has been requested.");
}
proxy.json
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"GetJustArtinAroundStorageLinkProxy": {
"matchCondition": {
"route": "/products/{category:alpha}/{id}",
"methods": [
"GET"
]
},
"backendUri": "https://<company-name>.azurewebsites.net/api/products/{category:alpha}?id={id}"
}
}
}
【问题讨论】:
标签: c# azure azure-functions