Azure的功能 - 调整存储在一个blob容器
我已经回答this question有关Azure的Webjob和调整大小作为Blob存储一个形象,所以我尝试使用Function App
Azure的功能 - 调整存储在一个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'.
此类型是否支持此类型?
是支持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故障(我们正在修复的错误),有时会导致问题。
我重试了,它的工作原理...我不得不强制重新编译。谢谢 – Thomas
您应该为Azure函数的设置下的集成功能使用输出,而不是自己创建blob。 –