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

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

问题描述

当您到达给定的Azure Function端点时,我想从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 Storage 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

如果您不愿意使用C#函数来代替NodeJ,则可以直接在输入绑定和流请求输出中使用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函数-NodeJS-作为流的响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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