0

私はImageResizer exampleに基づいている単純なAzure関数を構築しようとしていますが、Microsoft Cognitive Server Computer Vision APIを使用してサイズ変更を行います。私のAzureストレージ出力のBLOBは表示されません

私はAzure関数に移植したworking code for the Computer Vision APIを持っています。

すべてが正常に動作しているようです(エラーは表示されません)が、出力されたBLOBは保存されず、ストレージコンテナに表示されません。私が何をしているのか分からないのは、エラーがないからです。

マイAzureのストレージアカウントが 'thumbnailgenstorage' と呼ばれる

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

次のように

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); 
    } 
} 

My機能JSONが続き、それが'originals'という名前の2個のコンテナを持っていると私のCSX(C#の機能コード)がありますおよび'thumbs'。ストレージアカウントのキーはKGdcO+hjvARQvSwd2rfmdc+rrAsK0tA5xpE4RVNmXZgExCE+Cyk4q0nSiulDwvRHrSAkYjyjVezwdaeLCIb53g==です。

私はこれを理解するために私のキーを使用するのがとても嬉しいです! :)

+0

です。私はeth出力BLOBに正しく書いていたのかどうかはわかりません。これはうまくいくようです: '//レスポンスの読み取りと出力ストリームへの書き込み var responseBytes = response.Content.ReadAsByteArrayAsync()。Result; thumb.Write(responseBytes、0、responseBytes.Length); '。また、私のjsonでの出力パスは、現在、親指/%rand-guid%.jpg "、"です。私はそれが動作すると確信しているときにもう少しリサーチに行き、完全な答えを投稿してください。 –

+0

はい、正しいです。ストリームへの単純な割り当てはモデルではありません。あなたは提供されたストリームに書き込む必要があります。あなたは、他のすべてのバインディングをまたぐケースであることがわかります。 – mathewc

答えて

2

私はこれが今働いています。私は間違って出力ストリームを書いていました。

このソリューションは、「オリジナル」というAzureブロブストレージコンテナにブロブが到着したことをトリガーとし、Computer Vision APIを使用してスマートにイメージのサイズを変更し、「サムス」という別のブロブコンテナに格納します。 。ここで

は作業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 
} 
関連する問題