Azure函数 - NodeJS - 响应主体作为流

时间:2017-05-05 16:48:08

标签: node.js azure azure-functions

当您点击给定的Azure功能端点时,我想从Blob存储中返回一个文件。这个文件是二进制数据。

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

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

Azure Functions是否有办法将Stream用作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();
}

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:3)

虽然@Matt Manson的回答基于我提出问题的方式肯定是正确的,但以下代码段对于遇到这个问题的人来说可能更有用。

虽然我无法直接将Stream发送到响应主体,但我可以使用自定义流将数据捕获到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();
}

答案 2 :(得分:0)

我从上面的最后一条评论中尝试了@Doug的解决方案,在我的azure函数中添加了一些次要的mod,到目前为止,在尝试了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();
...
相关问题