Azure Functions - NodeJS - 作为流的响应主体 [英] Azure Functions - NodeJS - Response Body as a Stream

查看:9
本文介绍了Azure Functions - NodeJS - 作为流的响应主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您到达给定的 Azure 函数端点时,我想从 Blob 存储返回一个文件.这个文件是二进制数据.

I'd like to return a file from Blob Storage when you hit a given Azure Function end-point. This file is binary data.

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

Per the Azure Storage Blob docs, the most relevant call appears to be the following since its the only one that doesn't require writing the file to an interim file: getBlobToStream

但是,此调用获取 Blob 并将其写入流.

However this call gets the Blob and writes it to a stream.

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

Is there a way with Azure Functions to use a Stream as the value of res.body so that I can get the Blob Contents from storage and immediately write it to the response?

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

To add some code, trying to get something like this to work:

'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

Unfortunately we don't have streaming support implemented in NodeJS just yet - it's on the backlog: https://github.com/Azure/azure-webjobs-sdk-script/issues/1361

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

If you're not tied to NodeJ open to using a C# function instead, you can use the storage sdk object directly in your input bindings and stream request output, instead of using the intermediate object approach.

这篇关于Azure Functions - NodeJS - 作为流的响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆