我正在使用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) });
任何帮助,将不胜感激。我不知道如何在不将jpg保存到磁盘的情况下解码base64并从中生成流。
请尝试这样的事情:
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节点包才能将缓冲区转换为流。
streamifier