Azure WebJobs Blob触发器-多种大小 [英] Azure WebJobs Blob Trigger - multiple resizes

查看:49
本文介绍了Azure WebJobs Blob触发器-多种大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建C#Azure WebJob,它会在创建新的Blob时触发,以将上传的图像调整为三种不同的大小.我发现并遵循了这个很棒的教程.

I'm attempting to create C# Azure WebJob which is triggered on a new Blob creation to resize the uploaded image into three different sizes. I found and followed this great tutorial.

有两个部分,第一个部分起作用",但是由于创建三个新尺寸触发脚本而进入了递归循环,该脚本为三个新图像中的每个图像创建了三个实例,依此类推.这是有意的,以强调最终实施的必要性.

There are two sections, the first portion "works" but enters into a recursion loop as the creation of the three new sizes triggers the script, which creates three more instances for each of three new images, so forth and so forth. This was intentional, to highlight the need for the final implementation.

这是在Function.cs文件中起作用"的位置的初始递归循环代码:

Here is the initial recursion loop code which "works" location in the Functions.cs file:

public static void ResizeImagesW800([BlobTrigger("input/{name}.{ext}")] Stream input,
    [Blob("output/{name}-w800.{ext}", FileAccess.Write)] Stream output)
{
    ResizeImage(input, output, 800);
}

public static void ResizeImagesW500([BlobTrigger("input/{name}.{ext}")] Stream input,
    [Blob("output/{name}-w500.{ext}", FileAccess.Write)] Stream output)
{
    ResizeImage(input, output, 500);
}

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

以下是Visual Studio 2015给出错误的代码:

Here is the code which Visual Studio 2015 gives an error on:

public static void ResizeImagesTask(
    [BlobTrigger("input/{name}.{ext}")] Stream inputBlob,
    string name,
    string ext,
    IBinder binder)
{
    int[] sizes = { 800, 500, 250 };
    var inputBytes = inputBlob.CopyToBytes();
    foreach (var width in sizes)
    {
        var input = new MemoryStream(inputBytes);
        var output = binder.Bind<Stream>(new BlobAttribute($"output/{name}-w{width}.{ext}", FileAccess.Write));

        ResizeImage(input, output, width);
    }
}

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

此行引发错误:

 var inputBytes = inputBlob.CopyToBytes();

错误是:

CS1061: 'Stream' does not contain a definition for 'CopyToBytes' and no extension method 'CopyToBytes' accepting a first argument of type 'Stream' could be found (are you missing a using directive or an assembly reference?)

我尝试使用.NET 3.5、4.0、4.5、4.5.1、4.5.2、4.6、4.6.1作为目标框架,但是它们都抛出相同的错误.

I've tried using .NET 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1 as target frameworks, but all of them throw the same error.

此外,这是Functions.cs文件的using语句:

Also, here are the using statements for the Functions.cs file:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Storage;
using ImageResizer;

我在这里做错了什么?谢谢!

What am I doing wrong here? Thanks!

更新1

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Storage;
using ImageResizer;
using ImageResizer.ExtensionMethods;
using Microsoft.WindowsAzure.Storage.Blob;

namespace HilcoIndustrialAssetApiWebJob
{
    public class Functions
    {
        // output blolb sizes
        private static readonly int[] Sizes = { 800, 500, 250 };

        public static void ResizeImagesTask(
        [QueueTrigger("newfileuploaded")] string filename,
        [Blob("input/{queueTrigger}", FileAccess.Read)] Stream blobStream,
        [Blob("output")] CloudBlobContainer container)
        {
            // Extract the filename  and the file extension
            var name = Path.GetFileNameWithoutExtension(filename);
            var ext = Path.GetExtension(filename);

            Console.WriteLine("New Blob name -> " + name);

            // 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
                var blob = container.GetBlockBlobReference($"{name}_{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();
        }
    }
}

推荐答案

我猜该示例使用ImageResizer NuGet包. 您可以使用以下命令从VS2015安装它 安装包ImageResizer. 然后,如果您添加 使用ImageResizer.ExtensionMethods; 在您的代码中,您将获得用于扩展Stream对象的CopyToBytes方法. 希望这可以帮助 最好的祝福 斯特凡(Stéphane)

I guess the sample use ImageResizer NuGet package. You may install it from VS2015 with the command Install-Package ImageResizer. Then if you add using ImageResizer.ExtensionMethods; in your code, you'll get the CopyToBytes method extending the Stream object. Hope this helps Best regards Stéphane

这篇关于Azure WebJobs Blob触发器-多种大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆