问题描述
我需要为缩略图设置 Content-Type
.我已经尝试如下所示.但它不工作.仍然,它存储为流.
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 函数:
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
}
我在 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);
问题:你能告诉我如何在 Azure 函数上设置 Content-Type
吗?
Question: Can you tell me how to set Content-Type
on the Azure function?
附言我不是 Nodejs 开发人员.
p.s. I'm not a Nodejs developer.
推荐答案
很遗憾,节点的 blob 输出绑定不支持设置内容类型.一种选择是删除输出绑定并在您的本地使用 azure storage sdk节点函数,它应该为您提供所需的控制.
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.
如果使用 Http 触发器和输出绑定:
可以通过 content.res
访问类似 express 的res"对象,因此您需要 context.res 而不是
/stream.set
.设置context.res.type
.getBuffer
回调中返回的stream
对象是缓冲区,不是流,与http响应无关.
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.
需要注意的一点是,azure 函数还不支持从节点返回流 - 您需要拥有整个缓冲区(幸运的是,getBuffer 似乎返回了!)
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!)
这是一个 getBuffer
回调:
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);
}
});
这篇关于设置缩略图图像内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!