我的Azure存储输出blob永远不会出现

时间:2016-05-03 10:51:10

标签: azure-storage-blobs microsoft-cognitive azure-functions

我尝试构建一个基于ImageResizer example的简单Azure功能,但使用Microsoft Cognitive Server Computer Vision API进行调整大小。

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

这一切似乎都运行正常(没有错误),但我的输出blob永远不会被保存或显示在存储容器中。不确定我做错了什么,因为没有错误可以使用。

我的CSX(C#功能代码)如下

(([01]?[8-9]|20):[0-5][0-9])|21:00

我的函数json如下

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

我的Azure存储帐户名为' thumbnailgenstorage'它有两个名为'originals''thumbs'的容器。存储帐户密钥为{ "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 }

我很高兴人们可以使用我的钥匙来帮助我解决这个问题! :)

1 个答案:

答案 0 :(得分:2)

我现在就开始工作了。我错误地写了输出流。

此解决方案是一个Azure函数,它在blob到达名为“Originals”的Azure Blob存储容器中时触发,然后使用Computer Vision API智能调整图像大小并存储在名为的另一个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
}