如何在不本地保存的情况下将 base64 数据传输到可读的图像流中

How do I transfer base64 data into a readable image stream without saving locally(如何在不本地保存的情况下将 base64 数据传输到可读的图像流中)

本文介绍了如何在不本地保存的情况下将 base64 数据传输到可读的图像流中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with the Microsoft Bot Framework (hosting on Azure) using Node.js, and saving files locally isn't an option. I have image data in base64 format, and I need to transfer that data into a readable stream of an image file that I can pump into Azure using their Blob Storage API (which takes a stream).

            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 并从中生成流.

Any help would be appreciated. I can't figure out how to decode the base64 and make a stream out of it without saving a jpg onto my disk.

推荐答案

请尝试以下方法:

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 节点包来转换缓冲区流式传输.

I had to install streamifier node package to convert buffer to stream.

这篇关于如何在不本地保存的情况下将 base64 数据传输到可读的图像流中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在不本地保存的情况下将 base64 数据传输到可读的图像流中

基础教程推荐