Azure Functions - NodeJS - 作为流的响应主体

Azure Functions - NodeJS - Response Body as a Stream

我想 return 当你到达给定的 Azure 函数端点时,从 Blob 存储中获取一个文件。此文件为二进制数据。

根据 Azure 存储 Blob 文档,最相关的调用似乎是以下调用,因为它是唯一不需要将文件写入临时文件的调用: getBlobToStream

然而,此调用获取 Blob 并将其写入流。

Azure Functions 是否有办法使用流作为 res.body 的值,以便我可以从存储中获取 Blob 内容并立即将其写入响应?

要添加一些代码,尝试让这样的东西工作:

'use strict';
const   azure = require('azure-storage'),
        stream = require('stream');
const BLOB_CONTAINER = 'DeContainer';

module.exports = function(context){
    var file = context.bindingData.file;
    var blobService = azure.createBlobService();
    var outputStream = new stream.Writable();

    blobService.getBlobToStream(BLOB_CONTAINER, file, outputStream, function(error, serverBlob) {
        if(error) {
            FileNotFound(context);
        } else {
            context.res = {
                status: 200,
                headers: {

                },
                isRaw: true,
                body : outputStream
            };
            context.done();


        }
    });
}

function FileNotFound(context){
    context.res =  {
        status: 404,
        headers: {
            "Content-Type" : "application/json"
        },
        body : { "Message" : "No esta aqui!."}
    };
    context.done();
}

不幸的是,我们还没有在 NodeJS 中实现流式支持 - 它在积压中:https://github.com/Azure/azure-webjobs-sdk-script/issues/1361

如果您不依赖 NodeJ 而是使用 C# 函数,您可以直接在输入绑定和流请求输出中使用存储 sdk 对象,而不是使用中间对象方法。

虽然根据我提出问题的方式,@Matt Manson 的回答肯定是正确的,但以下代码片段可能对偶然发现此问题的人更有用。

虽然我无法将流直接发送到响应主体,但我可以使用自定义流将数据捕获到 Uint8Array 中,然后将其发送到响应主体。

注意:如果文件真的很大,这将占用大量内存。

'use strict';
const   azure = require('azure-storage'),
        stream = require('stream');
const BLOB_CONTAINER = 'deContainer';

module.exports = function(context){
    var file = context.bindingData.file;
    var blobService = azure.createBlobService();
    var outputStream = new stream.Writable();
    outputStream.contents = new Uint8Array(0);//Initialize contents.

    //Override the write to store the value to our "contents"
    outputStream._write = function (chunk, encoding, done) {
        var curChunk = new Uint8Array(chunk);
        var tmp = new Uint8Array(this.contents.byteLength + curChunk.byteLength);
        tmp.set(this.contents, 0);
        tmp.set(curChunk, this.contents.byteLength);
        this.contents = tmp;
        done();
    };


    blobService.getBlobToStream(BLOB_CONTAINER, file, outputStream, function(error, serverBlob) {
        if(error) {
            FileNotFound(context);
        } else {
            context.res = {
                status: 200,
                headers: {

                },
                isRaw: true,
                body : outputStream.contents
            };
            context.done();
        }
    });//*/
}

function FileNotFound(context){
    context.res =  {
        status: 404,
        headers: {
            "Content-Type" : "application/json"
        },
        body : { "Message" : "No esta aqui!"}
    };
    context.done();
}

我尝试了上面最后一条评论中@Doug 的解决方案,在我的 azure 函数中做了一些小修改,到目前为止,在尝试了 20 种不同的想法之后,这是唯一一个真正将文件传送到浏览器的方法!谢谢@Doug...


const fs = require("fs");
const stream = require("stream");

...

                const AzureBlob = require('@[my_private_artifact]/azure-blob-storage');
                const azureStorage = new AzureBlob(params.connectionString);


                //Override the write to store the value to our "contents" <-- Doug's solution
                var outputStream = new stream.Writable();
                outputStream.contents = new Uint8Array(0);//Initialize contents.

                outputStream._write = function (chunk, encoding, done) {
                    var curChunk = new Uint8Array(chunk);
                    var tmp = new Uint8Array(this.contents.byteLength + curChunk.byteLength);
                    tmp.set(this.contents, 0);
                    tmp.set(curChunk, this.contents.byteLength);
                    this.contents = tmp;
                    done();
                };

                let azureSpeedResult = await azureStorage.downloadBlobToStream(params.containerName, params.objectId, outputStream);

                let headers = {
                    "Content-Length": azureSpeedResult.size,
                    "Content-Type": mimeType
                };

                if (params.action == "download") {
                    headers["Content-Disposition"] = "attachment; filename=" + params.fileName;
                } 

                context.res = {
                    status: 200,
                    headers: headers,
                    isRaw: true,
                    body: outputStream.contents
                };

                context.done();
...