0

我正嘗試構建基於ImageResizer example的簡單Azure功能,但使用Microsoft Cognitive Server Computer Vision API來執行調整大小。我的Azure存儲輸出blob從不出現

我有working code for the Computer Vision API我已經移植到Azure功能。

這一切似乎工作正常(沒有錯誤),但我的輸出blob永遠不會保存或顯示在存儲容器中。不知道我做錯了什麼,因爲沒有錯誤可以使用。

我CSX(C#函數代碼)如下

using System; 
using System.Text; 
using System.Net.Http; 
using System.Net.Http.Headers; 

public static void Run(Stream original, Stream thumb, TraceWriter log) 
{ 
    //log.Verbose($"C# Blob trigger function processed: {myBlob}. Dimensions"); 
    string _apiKey = "PutYourComputerVisionApiKeyHere"; 
    string _apiUrlBase = "https://api.projectoxford.ai/vision/v1.0/generateThumbnail"; 
    string width = "100"; 
    string height = "100"; 
    bool smartcropping = true; 

    using (var httpClient = new HttpClient()) 
    { 
     //setup HttpClient 
     httpClient.BaseAddress = new Uri(_apiUrlBase); 
     httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey); 

     //setup data object 
     HttpContent content = new StreamContent(original); 
     content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream"); 

     // Request parameters 
     var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartcropping}"; 

     //make request 
     var response = httpClient.PostAsync(uri, content).Result; 

     //log result 
     log.Verbose($"Response: IsSucess={response.IsSuccessStatusCode}, Status={response.ReasonPhrase}"); 

     //read response and write to output stream 
     thumb = new MemoryStream(response.Content.ReadAsByteArrayAsync().Result); 
    } 
} 

我的功能JSON是如下

{ 
    "bindings": [ 
    { 
     "path": "originals/{name}", 
     "connection": "thumbnailgenstorage_STORAGE", 
     "name": "original", 
     "type": "blobTrigger", 
     "direction": "in" 
    }, 
    { 
     "path": "thumbs/%rand-guid%", 
     "connection": "thumbnailgenstorage_STORAGE", 
     "type": "blob", 
     "name": "thumb", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

我的Azure存儲帳戶被稱爲 'thumbnailgenstorage',它有一個名爲'originals'兩個容器和'thumbs'。存儲帳戶密鑰是KGdcO+hjvARQvSwd2rfmdc+rrAsK0tA5xpE4RVNmXZgExCE+Cyk4q0nSiulDwvRHrSAkYjyjVezwdaeLCIb53g==

我很高興人們使用我的鑰匙來幫助我弄清楚這一點! :)

+0

我想我已經解決了這個問題。我不確定我是否正確寫入eth輸出blob。這似乎工作:'//讀取響應並寫入輸出流 var responseBytes = response.Content.ReadAsByteArrayAsync()。 thumb.Write(responseBytes,0,responseBytes.Length);'。此外,我的json中的輸出路徑現在是'「thumbs /%rand-guid%.jpg」,「。當我確信它能正常工作時,繼續研究一下併發布完整答案 –

+0

是的,這是正確的 - 對流的簡單分配不是模型,因爲它不是語言意義上的「out」參數。你必須寫信給提供的流。你會發現所有其他綁定都是這種情況。 – mathewc

回答

2

我現在得到這個工作。我錯誤地寫了輸出流。

該解決方案是一個Azure函數,它在blob到達名爲'Originals'的Azure Blob存儲容器時觸發,然後使用Computer Vision API巧妙地調整圖像大小並將其存儲在名爲'Thumbs'的不同Blob容器中。

這裏是工作CSX(C#腳本):

using System; 
using System.Text; 
using System.Net.Http; 
using System.Net.Http.Headers; 

public static void Run(Stream original, Stream thumb, TraceWriter log) 
{ 
    int width = 320; 
    int height = 320; 
    bool smartCropping = true; 
    string _apiKey = "PutYourComputerVisionApiKeyHere"; 
    string _apiUrlBase = "https://api.projectoxford.ai/vision/v1.0/generateThumbnail"; 

    using (var httpClient = new HttpClient()) 
    { 
     httpClient.BaseAddress = new Uri(_apiUrlBase); 
     httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey); 
     using (HttpContent content = new StreamContent(original)) 
     { 
      //get response 
      content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream"); 
      var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartCropping.ToString()}"; 
      var response = httpClient.PostAsync(uri, content).Result; 
      var responseBytes = response.Content.ReadAsByteArrayAsync().Result; 

      //write to output thumb 
      thumb.Write(responseBytes, 0, responseBytes.Length); 
     } 
    } 
} 

這裏是集成JSON

{ 
    "bindings": [ 
    { 
     "path": "originals/{name}", 
     "connection": "thumbnailgenstorage_STORAGE", 
     "name": "original", 
     "type": "blobTrigger", 
     "direction": "in" 
    }, 
    { 
     "path": "thumbs/{name}", 
     "connection": "thumbnailgenstorage_STORAGE", 
     "name": "thumb", 
     "type": "blob", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
}