【问题标题】:Azure Functions - route template with fileAzure Functions - 带有文件的路由模板
【发布时间】:2017-07-27 02:17:35
【问题描述】:

我创建了一个接收 HTTP 请求并返回 HTTP 请求的 Azure 函数。功能:

  1. 接受 HTTP 请求
  2. 使用在 n 分钟/小时后过期的共享访问签名创建指向 Blob 存储中某个位置的 URI
  3. 返回 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


    【解决方案1】:

    目前,HttpTrigger 存在限制,不支持带扩展的请求(详情请参阅this)。

    如问题中所述,您可以使用代理来解决此限制,但您确实需要从您的路由中删除 alpha 约束。

    这是一个示例代理配置,它将把上面的 id 作为查询字符串转发:

    {
        "$schema": "http://json.schemastore.org/proxies",
        "proxies": {
            "Test": {
                "matchCondition": {
                    "route": "products/{category}/{id}"
                },
                "backendUri": "https://<functionapp>.azurewebsites.net/api/<function>?id={id}"
            }
        }
    }
    

    您可以调整以上内容以满足您的要求,但这应该会给您所需的行为。

    【讨论】:

    • 这似乎不起作用。它返回 500 - 内部服务器错误。我确保将密钥传递给 x-function-key 标头和代码查询参数中的函数。此外,我注意到 {category:alpha} 不允许使用破折号,所以像“break-pads”这样的东西会返回 404。我试图打开日志记录,以便它会推送到我的 blob 存储,但这并没有出现工作。我的日志容器仍然是空的。
    • 我根据您的反馈更新了问题的详细信息。
    • 我用上面的配置测试过,没有遇到问题。您是否能够使用其直接 URL(不通过代理)调用您的函数?您能否分享一个调用 ID、区域和错误的大致时间,以便我们查看您的日志?
    • 问题在于代理定义中的 {category:alpha}。只需在路由和 backendUri 中使用 {category}。
    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多