0

我最近開始使用azure函數將base64字符串轉換爲圖像並將此圖像存儲在blob中。 但是我沒有找到關於如何將特定文件類型寫入blob的任何信息,就像我可以通過fs.writeFile()在Node中正常執行的操作一樣。Azure函數Node.js:將圖像寫入Blob

有人可以告訴我,如果它甚至可以做到這一點與Azure函數的正常「輸出」? 目標是我可以將base64字符串轉換爲圖像並將其保存在一個blob中。

module.exports = function (context, input) { 

    var image = input; 
    var bitmap = new Buffer(image, 'base64'); 

    //base64 string to test: "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" 

    function decodeBase64Image(dataString) { 
     var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/), 
     response = {}; 

     if (matches.length !== 3) { 
      return new Error('Invalid input string'); 
     } 

     response.type = matches[1]; 
     response.data = new Buffer(matches[2], 'base64'); 

     return response; 
    } 

    var imageBuffer = decodeBase64Image(input); 

    //context.bindings.outputBlob = {'test2.jpg', imageBuffer.data}; 

    context.done(null, imageBuffer); 
}; 

我functions.json看起來是這樣的:

{ 
    "bindings": [ 
    { 
     "type": "manualTrigger", 
     "direction": "in", 
     "name": "input" 
    }, 
    { 
     "type": "blob", 
     "name": "outputBlob", 
     "path": "logo/{rand-guid}", 
     "connection": "npsmonitordev_STORAGE", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

回答

0

是的,這是可能的。

在我的測試,我只設置outputBlob等於imageBuffer.data如下:

context.bindings.outputBlob = imageBuffer.data; 
context.done(); 

,在我function.json我有這樣的:

{ 
    "bindings": [ 
    //... 
    { 
     "type": "blob", 
     "name": "outputBlob", 
     "path": "outcontainer/{rand-guid}.png", 
     "connection": "aaronchstorage_STORAGE", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

這將緩衝區中的數據保存爲一個BOLB文件Azure存儲。

enter image description here