从 azure blob 下载 excel 文件并处理其数据,而无需将文件保存到本地目录
Download a excel file from azure blob and process its data without needing to save file to local directory
我想从 azure blob 下载一个 excel 文件并使用 'xlsx' npm 模块处理它的数据。
我通过将文件保存到我的 node.js 服务器上的本地目录来实现这一点。
但我必须在不需要将文件本地保存在服务器上的情况下实现它。
我该如何实现?
以下是我的js文件使用-下载到本地目录的方法。
const xlsx = require('xlsx');
const azureStorageConfig = {
accountName: "",
accountKey: "",
blobURL: "",
containerName: "test-container"
};
let fileName = "test_blob.xlsx";
const downloadBlob = async (blobName, downloadFilePath) => {
return new Promise((resolve, reject) => {
const name = path.basename(blobName);
const blobService = azureStorage.createBlobService(azureStorageConfig.accountName,azureStorageConfig.accountKey);
blobService.getBlobToLocalFile(azureStorageConfig.containerName,blobName,`${downloadFilePath}${name}`, function(error, serverBlob) {
if (error) {
reject(error);
} else {
resolve(downloadFilePath);
}
});
});
};
downloadBlob(fileName,'./local_dir/').then((downloadFilePath)=>{
parseExcel(downloadFilePath + fileName);
});
const parseExcel = function(downloaded_file_path){
let workbook = xlsx.readFile(downloaded_file_path);
// Parse further
}
执行不需要将文件保存到本地目录的过程时,此代码将如何更改?
作为您的参考,这是我的想法和您需要的示例代码,如下所示。
使用 SAS 令牌
生成一个 blob url
var azure = require('azure-storage');
var accountName = '<your account name>';
var accountKey = '<your account key>';
var blobService = azure.createBlobService(accountName, accountKey);
var containerName = 'test-container';
var blobName = 'test_blob.xlsx';
var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
Start: startDate,
Expiry: expiryDate
}
};
var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
var sasUrl = blobService.getUrl(containerName, blobName, token);
通过 request
读取 blob 主体字节,然后使用 XLSX.read(data, read_opts)
将 blob 主体解析为 Uint8Array
。
var request = require('request');
var XLSX = require('xlsx');
request(sasUrl, {encoding: null}, function (error, response, body) {
var workbook = XLSX.read(body, {type:"buffer"});
console.log(workbook.Sheets.Sheet1);
});
希望对您有所帮助。
我想从 azure blob 下载一个 excel 文件并使用 'xlsx' npm 模块处理它的数据。 我通过将文件保存到我的 node.js 服务器上的本地目录来实现这一点。
但我必须在不需要将文件本地保存在服务器上的情况下实现它。
我该如何实现?
以下是我的js文件使用-下载到本地目录的方法。
const xlsx = require('xlsx');
const azureStorageConfig = {
accountName: "",
accountKey: "",
blobURL: "",
containerName: "test-container"
};
let fileName = "test_blob.xlsx";
const downloadBlob = async (blobName, downloadFilePath) => {
return new Promise((resolve, reject) => {
const name = path.basename(blobName);
const blobService = azureStorage.createBlobService(azureStorageConfig.accountName,azureStorageConfig.accountKey);
blobService.getBlobToLocalFile(azureStorageConfig.containerName,blobName,`${downloadFilePath}${name}`, function(error, serverBlob) {
if (error) {
reject(error);
} else {
resolve(downloadFilePath);
}
});
});
};
downloadBlob(fileName,'./local_dir/').then((downloadFilePath)=>{
parseExcel(downloadFilePath + fileName);
});
const parseExcel = function(downloaded_file_path){
let workbook = xlsx.readFile(downloaded_file_path);
// Parse further
}
执行不需要将文件保存到本地目录的过程时,此代码将如何更改?
作为您的参考,这是我的想法和您需要的示例代码,如下所示。
使用 SAS 令牌
生成一个 blob urlvar azure = require('azure-storage'); var accountName = '<your account name>'; var accountKey = '<your account key>'; var blobService = azure.createBlobService(accountName, accountKey); var containerName = 'test-container'; var blobName = 'test_blob.xlsx'; var startDate = new Date(); var expiryDate = new Date(startDate); expiryDate.setMinutes(startDate.getMinutes() + 100); startDate.setMinutes(startDate.getMinutes() - 100); var sharedAccessPolicy = { AccessPolicy: { Permissions: azure.BlobUtilities.SharedAccessPermissions.READ, Start: startDate, Expiry: expiryDate } }; var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy); var sasUrl = blobService.getUrl(containerName, blobName, token);
通过
request
读取 blob 主体字节,然后使用XLSX.read(data, read_opts)
将 blob 主体解析为Uint8Array
。var request = require('request'); var XLSX = require('xlsx'); request(sasUrl, {encoding: null}, function (error, response, body) { var workbook = XLSX.read(body, {type:"buffer"}); console.log(workbook.Sheets.Sheet1); });
希望对您有所帮助。