2017-05-05 14 views
2

特定のAzure関数のエンドポイントにヒットしたときにBlobストレージからファイルを返したいと思います。このファイルはバイナリデータです。 Azureストレージブロブのドキュメント毎Azure関数 - NodeJS - ストリームとしての応答本体

中間ファイルへのファイルの書き込みを必要としないその一つだけ以来、最も関連性の高いコールは、次のように表示されます。 getBlobToStream

しかし、この呼び出しはブロブを取得それをストリームに書き込みます。

Azureがストリームを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(); 
} 

答えて

6

残念ながら、我々はまだNodeJSに実装サポートをストリーミング持っていない - それはバックログにあります:https://github.com/Azure/azure-webjobs-sdk-script/issues/1361

場合NodeJにC#関数を使用する代わりに、中間オブジェクトアプローチを使用する代わりに、入力バインディングとストリーム要求出力で直接ストレージsdkオブジェクトを使用できます。

+0

私はそれが答えではないことを期待していましたが、それにも感謝しています。私はこれを回避する方法を見ていきます。 – Doug

1

@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(); 
} 
関連する問題