如何将base64数据传输到可读图像流而不保存本地

时间:2017-07-06 15:01:32

标签: javascript node.js file azure base64

我正在使用Node.js使用Microsoft Bot Framework(在Azure上托管),并且无法在本地保存文件。我有base64格式的图像数据,我需要将这些数据传输到图像文件的可读流中,我可以使用他们的Blob存储API(需要流)将其输入Azure。

            var base64ImageData; //my base64 image

            //Create myStream here

            //Then send to Azure
            blobService.createBlockBlobFromStream('mycontainer', 
            nameForBlob, myStream, fileSize, 
            function (error, result, response){ 
                if(!error)
                    console.log(response);
                else
                    console.log(error)
            });

任何帮助将不胜感激。我无法弄清楚如何解码base64并从中创建一个流而不将jpg保存到我的磁盘上。

1 个答案:

答案 0 :(得分:1)

请尝试这样的事情:

var azureStorage = require('azure-storage');
var streamifier = require('streamifier');

var base64String = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEiSURBVDhPxZIhT0NBEITnoBKBrEQgEUhISEiqKiFUYJDI/gBEc8VVY5AYHKKGBIEAVBFN+A8EUVFZUdGk/XbvGtqmpS8ImGTu5vbd7N3tPv0Pog5gzeSGB4oiqgSbCnphNbRQsKEQorbY/YCqaqwrXatl4WIJosrsfELtw3vucOFxsP4J6eRnlJnfOf3S4xk/J0hmO3kPfmE+5er+9ilSAqtoU203TGEFC7pDHcFBNvf82wxSgqAzxhPmDsbdHLtl9FZhbmDuul5AKmLUNuoDtQMH8BGeQ0OXBIckGOX1HL67ELlq6m8pBRyjbF56umEzz9KbPnXM9qBKjhhuMFsdVmKxC/ZzvCbpVW9kvRLzCeydY/9J+sx11laPXyB63/8C0gQlpj3Fc3O2WAAAAABJRU5ErkJggg==';
var blobService = azureStorage.createBlobService('account-name', 
    'account-key');
var buffer = Buffer.from(base64String, 'base64');
var stream = streamifier.createReadStream(buffer);
blobService.createBlockBlobFromStream('container-name', 'checked.png', stream, buffer.byteLength, function(error, response) {
        if (error) {
            console.log('Error!');
            console.log(error);
        } else {
            console.log('Blob uploaded successfully!');
            console.log(response);
        }
    });

我必须安装streamifier节点包才能将缓冲区转换为流。