Uint8array在nodejs

时间:2017-07-10 09:23:56

标签: javascript node.js azure mean-stack node-streams

我正在使用具有角度前端的节点js api服务。我有一个功能,我需要将一些文件从谷歌驱动器迁移到天蓝色存储。我在节点中实现了azure blobService 交互。我使用XHR请求从谷歌驱动器以角度下载文档,将其转换为Uint8array,然后将其传递给节点js服务以上传到azure。我使用方法 blobService.createBlockBlobFromStream() 上传到azure。这需要可读流作为输入参数。我是节点js和节点流的新手。如何将Uint8array对象转换为可读流?如果不是Uint8array,我可以使用其他类型来简化转换吗?我已粘贴下面的代码。提前感谢所有的帮助...

角色代码

function requestXHR1(url, accessToken, fileId, type) {
                var xmlHttpReq = new XMLHttpRequest()
                xmlHttpReq.onload = function () {                       

                    uploadBlob(fileId, type, new Uint8Array(xmlHttpReq.response))
                        .then(function (data) {
                            migI++;
                            console.log("total: " + migI + ", uploaded fileId " + fileId);
                        },
                        function (error) {
                            console.log("error uploading fileId " + fileId);
                        });

                }

                xmlHttpReq.open('GET', url, true);
                xmlHttpReq.setRequestHeader('Authorization', 'Bearer ' + accessToken);
                xmlHttpReq.responseType = 'arraybuffer';



                xmlHttpReq.send(null);

}

function uploadBlob(fileName, contentType, file) {
        return $http({
            method: 'POST',
            url: config.baseUrl + 'uploadfile',
            data: { fileName: fileName, contentType: contentType, file: file },
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }).then(function (data) {
        }, function (error) {
        });
}

Node.js代码

function uploadBlob(req, res) {
  var blobSvc = azure.createBlobService(config.azureStorageAccount, config.azureSharedKey);
  var fileStream = ConvertToReadableStream(file); //How do I write this function?

  blobSvc.createBlockBlobFromStream(config.azureContainer, req.body.fileName, fileStream,
    req.body.file.length, { contentType: req.body.contentType, blockSize: customBlockSize }, function (error, result, response) {
        if (!error) {
            console.log("blob uploaded");
            res.status(200).send("success");
        }
        else
            res.send("failure");
    })
}

0 个答案:

没有答案