gpt4 book ai didi

c# - Azure 函数 - 调整存储在 blob 容器中的图像大小

转载 作者:行者123 更新时间:2023-11-30 17:38:17 25 4
gpt4 key购买 nike

我已经回答了this question与 Azure Webjob 和调整存储为 blob 的图像大小相关,因此我尝试使用 Function App

来做同样的事情

每次上传新的 blob 时,我都会发送一条新的队列消息。我的函数由队列消息触发并绑定(bind)到上传的 blob。我还有第二个输入绑定(bind),它绑定(bind)到另一个 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 受支持。我现在自己尝试了一个快速示例,下面的函数对我有用,使用上面显示的相同绑定(bind)元数据。我还在 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 故障(我们正在修复的错误),有时会导致问题。

关于c# - Azure 函数 - 调整存储在 blob 容器中的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com