问题
I need to set Content-Type
for the thumbnail image. I have tried as shown below.But it is not working.Still, it stores as a stream.
Azure function:
index.json
var Jimp = require("jimp");
module.exports = (context, myBlob) => {
// Read image with Jimp
Jimp.read(myBlob).then((image) => {
// Manipulate image
image
.resize(200, Jimp.AUTO)
.greyscale()
.getBuffer(Jimp.MIME_JPEG, (error, stream) => {
if (error) {
context.log(`There was an error processing the image.`);
context.done(error);
}
else {
context.log(`Successfully processed the image`);
stream.set("Content-Type", Jimp.MIME_JPEG); // here need to set the Content-Type
context.done(null, stream);
}
});
});
};
function.json
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "project2-photos-original/{name}",
"connection": "thumbnailfunction_STORAGE",
"dataType": "binary"
},
{
"type": "blob",
"name": "$return",
"path": "project2-photos-thumbnail/{name}",
"connection": "thumbnailfunction_STORAGE",
"direction": "out"
}
],
"disabled": false
}
I have seen the same implementation like this on NodeJs
var Jimp = require("jimp");
var express = require("express");
var app = express();
app.get("/my-dynamic-image", function(req, res){
Jimp.read("lenna.png", function(err, lenna) {
lenna.resize(64, 64).quality(60).getBuffer(Jimp.MIME_JPEG, function(err, buffer){
res.set("Content-Type", Jimp.MIME_JPEG);
res.send(buffer);
});
});
});
app.listen(3000);
Question: Can you tell me how to set Content-Type
on the Azure function?
p.s. I'm not a Nodejs developer.
回答1:
EDIT:
Unfortunately the blob output binding for node does not support setting a content type. One option would be to drop the output binding and use the azure storage sdk natively in your node function which should give you the control you need.
If using an Http trigger and output binding:
An express-like 'res' object can be accessed via content.res
, so instead of stream.set
you'll want context.res.set
/ context.res.type
. The stream
object returned in the getBuffer
callback is a buffer, not a stream, and has nothing to do with the http response.
One thing to note is that azure functions does not support returning of streams from node yet - you'll need to have the entire buffer (which, luckily, getBuffer appears to return!)
Here is a getBuffer
callback:
function(err, buffer){
if (err) {
context.log("There was an error processing the image.");
context.done(err);
}
else {
context.log("Successfully processed the image");
// set content type to Jimp.MIME_JPEG
context.res.type(Jimp.MIME_JPEG)
// send the raw response (don't apply any content negotiation)
context.res.raw(buffer);
}
});
来源:https://stackoverflow.com/questions/41858321/set-thumbnail-image-content-type