2

我已經回答this question有關Azure的Webjob和調整大小作爲Blob存儲一個形象,所以我嘗試使用Function AppAzure的功能 - 調整存儲在一個blob容器

每當一個新的做同樣的圖像blob被上傳,我發送一個新的隊列消息。 我的函數由隊列消息觸發並綁定到上傳的blob。我還有第二個輸入綁定,綁定到另一個CloudBlobContainer,以便能夠將新的調整大小的圖像上傳到另一個Blob容器。

我的功能看起來像這樣:

#r "System.Web" 
using System.IO; 
using System.Web; 
using ImageResizer; 
using Microsoft.Azure.WebJobs; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Blob; 

private static readonly int[] Sizes = { 800, 500, 250 }; 

public static void Run(string filename, Stream blobStream, CloudBlobContainer container, TraceWriter log) 
{ 
    log.Verbose($"C# Queue trigger function processed: {filename}"); 
    // Extract the filename and the file extension 
    var name = Path.GetFileNameWithoutExtension(filename); 
    var ext = Path.GetExtension(filename); 

    // Get the mime type to set the content type 
    var mimeType = MimeMapping.GetMimeMapping(filename); 

    foreach (var width in Sizes) 
    { 
     // Set the position of the input stream to the beginning. 
     blobStream.Seek(0, SeekOrigin.Begin); 

     // Get the output stream 
     var outputStream = new MemoryStream(); 
     ResizeImage(blobStream, outputStream, width); 

     // Get the blob reference 
     CloudBlockBlob blob = container.GetBlockBlobReference($"{name}-w{width}.{ext}"); 

     // Set the position of the output stream to the beginning. 
     outputStream.Seek(0, SeekOrigin.Begin); 
     blob.UploadFromStream(outputStream); 

     // Update the content type => don't know if required 
     blob.Properties.ContentType = mimeType; 
     blob.SetProperties(); 
    }    
} 

private static void ResizeImage(Stream input, Stream output, int width) 
{ 
    var instructions = new Instructions 
    { 
     Width = width, 
     Mode = FitMode.Carve, 
     Scale = ScaleMode.Both 
    }; 
    var imageJob = new ImageJob(input, output, instructions); 

    // Do not dispose the source object 
    imageJob.DisposeSourceObject = false; 
    imageJob.Build(); 
} 

關聯function.json文件:

{ 
"bindings": [ 
    { 
    "queueName": "newfileuploaded", 
    "connection": "crazytunastorageaccount_STORAGE", 
    "name": "filename", 
    "type": "queueTrigger", 
    "direction": "in" 
    }, 
    { 
    "path": "input-images/{queueTrigger}", 
    "connection": "crazytunastorageaccount_STORAGE", 
    "name": "blobStream", 
    "type": "blob", 
    "direction": "in" 
    }, 
    { 
    "name": "container", 
    "type": "blob", 
    "path": "output-images", 
    "connection": "crazytunastorageaccount_STORAGE", 
    "direction": "in" 
    } 
], 
"disabled": false 
} 

而且project.json文件:

{ 
"frameworks": { 
    "net46":{ 
    "dependencies": { 
     "ImageResizer": "4.0.5", 
     "WindowsAzure.Storage": "4.3.0" 
    } 
    } 
} 
} 

現在,當我編譯功能,我總是出現此錯誤:

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.ResizeBlobImage'. Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer'.

此類型是否支持此類型?

+0

您應該爲Azure函數的設置下的集成功能使用輸出,而不是自己創建blob。 –

回答

1

是支持CloudBlobContainer。我現在嘗試了一個快速示例,下面的函數適用於我,使用上面顯示的相同的綁定元數據。我也在project.json中使用相同版本的WebJobs SDK。

using System; 
using Microsoft.WindowsAzure.Storage.Blob; 

public static void Run(
    string blobTrigger, Stream inputBlob, Stream outputBlob, 
    CloudBlobContainer container, TraceWriter log) 
{ 
    log.Info($"Container name: {container.Name}"); 

    log.Info($"C# Blob trigger function processed {blobTrigger}"); 
    inputBlob.CopyTo(outputBlob);   
} 

不知道爲什麼這不適合你。我偶爾會看到一些Portal故障(我們正在修復的錯誤),有時會導致問題。

+0

我重試了,它的工作原理...我不得不強制重新編譯。謝謝 – Thomas