调整存储在Blob容器中的图像的大小

调整存储在Blob容器中的图像的大小

本文介绍了Azure功能-调整存储在Blob容器中的图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经回答了这个问题与Azure Webjob有关,并调整了以斑点形式存储的图像的大小,因此我尝试使用Function App

I've answered this question related to Azure Webjob and Resizing a image stored as a blob and so I am trying to do the same using a Function App

每次上传新的Blob时,我都会发送一条新的队列消息.我的功能由队列消息触发,并绑定到上传的Blob.我还有第二个输入绑定,它绑定到另一个CloudBlobContainer,以便能够将新调整大小的图像上传到另一个blob容器.

Each time a new blob is uploaded, I send a a new queue message.My function is triggered by the queue message and bind to the uploaded blob. I also have a second input binding that binds to another CloudBlobContainer to be able to upload new resized images to another blob container.

我的功能如下:

#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"
    }
  }
 }
}

现在,当我编译函数时,总是会出现此错误:

Now when I compiled the function, I always got this error:

目前是否支持该类型?

推荐答案

支持CloudBlobContainer.我现在自己尝试了一个快速示例,下面的函数对我有用,使用了上面显示的相同绑定元数据.我还在project.json中使用了相同版本的WebJobs SDK.

Yes CloudBlobContainer is supported. I tried a quick sample myself now, and the below function works for me, using the same binding metadata you showed above. I'm also using the same version of the WebJobs SDK in project.json.

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故障(我们正在修复的错误),这些故障有时会引起问题.

Not sure why this wasn't working for you. I have seen some Portal glitches from time to time (bugs we're fixing) that sometimes cause issues.

这篇关于Azure功能-调整存储在Blob容器中的图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:42