Azure功能 - 使用文件

时间:2017-07-27 02:17:35

标签: c# azure azure-functions

我创建了一个Azure Function,它接收HTTP请求并返回HTTP请求。功能:

  1. 接受HTTP请求
  2. 使用共享访问签名在blob存储中创建一个URI,该签名在n分钟/小时内到期
  3. 返回302状态代码,其中位置标头设置为将在n分钟/小时内过期的URI
  4. 当我在查询参数中放置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相关的问题,但是这个问题不适用于函数。

    Azure Functions Proxy - route to storage account

    使用此Azure Functions HTTP and webhook bindings示例,并滚动到自定义HTTP端点部分,我使用以下代码创建了另一个函数:

    Function.json - id是否从int更改?到alpha

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

    run.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 - &#34;已请求id = abc的测试项目。&#34;

    但是,如果您将其更改为products / test / abcd.jpg,则会响应:

    404 - 您要查找的资源已被删除,名称已更改或暂时不可用。

    这与我在我创建的其他函数中看到的行为相同。

    有没有人知道这是一个像代理示例的错误,还是我的路线看起来不一样?我再次使用查询参数,但是当我将变量移动到路径模板时,它会失败。

    已编辑 - 根据反馈添加了文件 function.json

    {
      "bindings": [
        {
          "name": "req",
          "type": "httpTrigger",
          "direction": "in",
          "methods": [
            "get"
          ],
          "route": "products/{category:alpha}",
          "authLevel": "function"
        },
        {
          "name": "$return",
          "type": "http",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    

    run.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.");
    }
    

    proxies.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}"
            }
        }
    }
    

1 个答案:

答案 0 :(得分:2)

目前,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}"
        }
    }
}

您可以调整上述内容以符合您的要求,但这可以为您提供所需的行为。