问题
I am trying to resize the images uploaded to my container in order to create thumbnails and various other versions of my images for my site.
The images i upload have to correct content-type "image/jpeg" but when i create a new version of them using the code below it turns out as "application/octet-stream".
What am i missing here?
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));
}
Edit: The solution.
#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);
}
回答1:
When you use the stream output, functions will default your content-type to application/octet-stream.
Use one of the ICloudBlob types, which should allow you to specify the content type of your blob.
Here's a cheatsheet of types you can bind to as parameters: https://jhaleyfiles2016.blob.core.windows.net/public/Azure%20WebJobs%20SDK%20Cheat%20Sheet%202014.pdf
回答2:
Here's a complete working example of an image-resizing Azure Function:
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"
}
}
}
}
来源:https://stackoverflow.com/questions/43945843/setting-the-content-type-of-a-blob-in-azure-functions-app-blobtrigger