首页 > 解决方案 > 如何从 node.js 中的 blobService.getBlobToStream 返回结果?

问题描述

我正在尝试将 excel 存储在 azure blob 中。我成功地将文件读入文件' testing.xlsx '。现在,我想用这个excel文件(' testing.xlsx ')出来blobService.getBlobToStream(),但是做不到。

blobService.getBlobToStream(container, blobname, fs.createWriteStream('testing.xlsx'), function (error, result) {
              if(!error) {}
});

标签: node.jsazure-storage

解决方案


我尝试使用以下节点包编写示例代码azure-storage,然后成功运行它,没有任何错误将我的 blob 下载到 excel 文件testing.xlsx

var azure = require('azure-storage');
var fs = require('fs');
var accountName = '<storage account name>';
var accountKey = '<storage account key>';
var blobService = azure.createBlobService(accountName, accountKey);

var containerName = 'test';
var blobName = 'testing.xlsx';
var localFileName = 'testing.xlsx';

var writable = fs.createWriteStream(localFileName);
blobService.getBlobToStream(containerName, blobName, writable, function(error, result) {
    if(error) {
        console.log(error)
    }else{
        console.log(result)
    }
})

它的结果和截图如下。

在此处输入图像描述

在此处输入图像描述

所以不知道你遇到了什么问题。如果您收到任何错误信息或其他任何错误信息,请更新您的帖子,让我知道以寻求帮助。


更新:

var writable = fs.createWriteStream(localFileName);
blobService.getBlobToStream(containerName, blobName, writable, function(error, result) {
    if(error) {
        console.log(error)
    }else{
        console.log(result)
    }
    fs.readFile(localFileName, function(err, data) {
        if(err) {
            console.error(err);
        }
        console.log(data);
    });
})

在此处输入图像描述


推荐阅读