设置缩略图内容类型

Set Thumbnail image Content-Type

我需要为缩略图设置Content-Type。我试过如图 below.But 它不是 working.Still,它存储为流。

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吗?

p.s。我不是 Nodejs 开发人员。

编辑:

很遗憾,节点的 blob 输出绑定不支持设置内容类型。一种选择是放弃输出绑定并在您的节点函数中本地使用 azure storage sdk,这应该为您提供所需的控制。

如果使用 Http 触发器和输出绑定:

可以通过 content.res 访问 express-like 'res' 对象,因此您需要 context.res.set / context.res.type 而不是 stream.set . stream 对象 return 在 getBuffer 回调中编辑的是缓冲区,而不是流,与 http 响应无关。

需要注意的一件事是 azure 函数尚不支持 return 从节点中获取流 - 您需要拥有整个缓冲区(幸运的是,getBuffer 似乎 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);
    }
});