簡體   English   中英

Azure Functions-帶有文件的路由模板

[英]Azure Functions - route template with file

我創建了一個Azure函數,該函數接收一個HTTP請求並返回一個HTTP請求。 功能:

  1. 接受HTTP請求
  2. 創建一個指向blob存儲中某個位置的URI,其共享訪問簽名在n分鍾/小時內過期
  3. 返回位置狀態頭設置為URI的302狀態代碼,它將在n分鍾/小時內到期

當我將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代理-路由到存儲帳戶

使用此Azure Functions HTTP和webhook綁定示例,並滾動到“ 自定義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-“已請求ID為abc的測試項目。”

但是,如果將其更改為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}"
        }
    }
}

目前,與HttpTrigger的限制,它不支持擴展的要求(見這個細節)。

如問題中所述,您可以使用代理來解決此限制,但是您確實需要從路線中刪除alpha約束。

這是一個示例代理配置,它將轉發您上面具有的id作為查詢字符串:

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

您可以調整上面的內容以使其符合您的要求,但這應該可以為您提供所需的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM