当前位置: 首页 > 知识库问答 >
问题:

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

益阳平
2023-03-14

我已经回答了这个与Azure Webjob和调整存储为blob的图像大小有关的问题,因此我尝试使用<code>函数App

每次上传新 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();
}

关联的<code>函数。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
}

以及<code>项目。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:无法将 Blob 绑定到键入 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer'。

目前支持这种类型吗?

共有1个答案

长孙阳泽
2023-03-14

是的,支持CloudBobContainer。我现在自己尝试了一个快速示例,下面的函数对我有用,使用您上面显示的相同绑定元数据。我还在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故障(我们正在修复的错误),这些故障有时会导致问题。

 类似资料:
  • Azure函数存储帐户Blob容器触发器 在我们的一个用例中,我正在为具有以下条件的存储帐户容器中的任何活动寻找Azure函数触发器 < li >具有特定命名约定的容器(名称如xxxx-input) < li >它应该自动检测是否创建了新的容器(具有特定的命名约定) < li>

  • 我正在处理一个Azure存储项目,其中我需要在容器中上传和下载blob,并在列表框中列出容器和blob。我无法在我的列表框中显示容器和blob。 最后是我调用上传、下载和列表方法的接口背后的代码:

  • 我试图使用Python在azure存储中创建blob容器。我正在使用MSDN提供的文档在我的python程序中集成azure blob存储。 代码如下: 第一次创建blob容器,但第二次就出错了。

  • 我有一个EventHubTrighted函数app。下面是方法签名的代码示例: @functionname(“foo”)@storageaccount(“foostorageRead”)public HttpResponseMessage run(@httptrigger(name=“req”,methods={httpmethod.post},authLevel=authorizationlev

  • 我们已经在azurereader2上使用图像大小调整器很长时间了,没有问题。在一个小版本发布后(这真的不应该与此有关),当我们试图通过大小调整器访问图像时,突然出现错误。 这只是我们测试环境中的一个问题,新版本和图像大小调整器等在生产中运行良好。生产和测试都托管在azure中,并且都使用自己的azure blob存储。 示例url:test。我的网站。com/images/pim/b6ffa894

  • 我试图通过Azure功能从Azure Blob存储中获取文件并在浏览器中显示它们。当我导航到url时,我可以设法下载文件,但我无法在浏览器中将它们显示为静态文件/图像。 我只想在浏览器中显示,而不是下载。 我尝试了一些sdk命令,但没有成功。以下是我尝试过的: 任何想法将不胜感激。谢谢!