簡體   English   中英

在 Azure 函數應用程序 blobtrigger 中設置 blob 的內容類型

[英]Setting the content-type of a blob in Azure functions app blobtrigger

我正在嘗試調整上傳到我的容器的圖像的大小,以便為我的網站創建縮略圖和各種其他版本的圖像。

我上傳的圖像必須更正內容類型“圖像/jpeg”,但是當我使用下面的代碼創建它們的新版本時,結果是“應用程序/八位字節流”。

我在這里缺少什么?

using ImageResizer;
using ImageResizer.ExtensionMethods;
 
public static void Run(Stream myBlob, string blobname, string blobextension, Stream outputBlob, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Size: {myBlob.Length} Bytes");
    
    var instructions = new Instructions
    {
        Width = 570,
        Mode = FitMode.Crop,
        Scale = ScaleMode.Both,
    };
    ImageBuilder.Current.Build(new ImageJob(myBlob, outputBlob, instructions));
}

編輯:解決方案。

#r "Microsoft.WindowsAzure.Storage"
using ImageResizer;
using ImageResizer.ExtensionMethods;
using Microsoft.WindowsAzure.Storage.Blob;

public static void Run(Stream myBlob, string blobname, string blobextension, CloudBlockBlob outputBlob, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Size: {myBlob.Length} Bytes");
    
    var instructions = new Instructions
    {
        Width = 570,
        Mode = FitMode.Crop,
        Scale = ScaleMode.Both
    };

    Stream stream = new MemoryStream();
    ImageBuilder.Current.Build(new ImageJob(myBlob, stream, instructions));
    stream.Seek(0, SeekOrigin.Begin);
 
    outputBlob.Properties.ContentType = "image/jpeg";
    outputBlob.UploadFromStream(stream);
}

當您使用流輸出時,函數會將您的內容類型默認為 application/octet-stream。

使用 iCloudBlob 類型之一,它應該允許您指定 Blob 的內容類型。

這是您可以綁定為參數的類型備忘單: https : //jhaleyfiles2016.blob.core.windows.net/public/Azure%20WebJobs%20SDK%20Cheat%20Sheet%202014.pdf

這是調整圖像大小的 Azure 函數的完整工作示例:

run.csx

#r "Microsoft.WindowsAzure.Storage"

using Microsoft.WindowsAzure.Storage.Blob;
using ImageResizer;
using ImageResizer.ExtensionMethods;

public static void Run(Stream imageStream, string blobName, CloudBlockBlob outputBlob, TraceWriter log)
{
    log.Info($"Function triggered by blob\n Name:{blobName} \n Size: {imageStream.Length} Bytes");

    var instructions = new Instructions
    {
        Width = 400,
        Height = 350,
        Mode = FitMode.Max,
        OutputFormat = OutputFormat.Jpeg,
        JpegQuality = 85
    };

    using (var outputStream = new MemoryStream())
    {
        ImageBuilder.Current.Build(new ImageJob(imageStream, outputStream, instructions));
        outputStream.Position = 0;
        outputBlob.Properties.ContentType = "image/jpeg";
        outputBlob.UploadFromStream(outputStream);
    }
}

function.json

{
  "bindings": [
    {
      "name": "imageStream",
      "type": "blobTrigger",
      "direction": "in",
      "path": "watched-container/{blobName}.jpg",
      "connection": "AzureWebJobsDashboard"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "output-container/{blobName}.jpg",
      "connection": "AzureWebJobsDashboard",
      "direction": "inout"
    }
  ],
  "disabled": false
}

project.json

{
"frameworks": {
  "net46":{
    "dependencies": {
      "ImageResizer": "4.0.5"
    }
  }
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM