将 ExcelJS 的输出上传到 NodeJS 中的 Azure Blob 存储
Uploading output of ExcelJS to Azure Blob Storage in NodeJS
所以我正在使用 ExcelJS 创建一个 Excel 文件。目前我正在将文件保存到磁盘。我添加到文件中的数据来自 API 调用。
let options = {
filename: 'pathtofile',
useStyles: true,
useSharedStrings: true
}
let workbook = new Excel.stream.xlsx.WorkbookWriter(options)
let sheet = workbook.addWorksheet('myWorksheet')
// add some data to the file
sheet.commit()
workbook.commit()
我现在想更改它以便将其上传到 Azure Blob 存储。 ExcelJS 允许指定 stream
而不是要提交的 filename
。所以我决定使用 Memory Streams JS 所以现在选项看起来像这样。
let stream = new streams.WritableStream()
let options = {
stream: stream,
useStyles: true,
useSharedStrings: true
}
通过阅读 Azure 文档,两种最合适的方法似乎是 createBlockBlobFromStream()
和 createWriteStreamToBlockBlob()
,它们似乎都需要可读流(如果我错了请纠正我)。这是我卡住的地方,因为我有一个使用 ExcelJS 的可写流,但我需要一个用于 Azure Blob 存储的可读流。显然我仍然可以将文件写入磁盘;从文件创建一个可读流;然后在上传到 Azure Blob 存储后最终删除该文件,但这会产生很大的开销。我是不是在用一种非常迂回的方式来解决这个问题?
Streaming XLSX Writer部分有说明:
If neither stream nor filename is specified in the options, the workbook writer will create a StreamBuf object that will store the contents of the XLSX workbook in memory. This StreamBuf object, which can be accessed via the property workbook.stream, can be used to either access the bytes directly by stream.read() or to pipe the contents to another stream.
因此您可以尝试以下代码片段,而无需在 options
中设置 filename
:
let blobSvc = azure.createBlobService();
let options = {
useStyles: true,
useSharedStrings: true
}
let workbook = new Excel.stream.xlsx.WorkbookWriter(options)
let sheet = workbook.addWorksheet('myWorksheet')
sheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 32 },
{ header: 'D.O.B.', key: 'DOB', width: 10, outlineLevel: 1 }
];
sheet.commit()
workbook.commit()
workbook.stream.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer','test.xlsx',function(err,result){
if(err)console.log(err)
console.log(result);
}))
所以我正在使用 ExcelJS 创建一个 Excel 文件。目前我正在将文件保存到磁盘。我添加到文件中的数据来自 API 调用。
let options = {
filename: 'pathtofile',
useStyles: true,
useSharedStrings: true
}
let workbook = new Excel.stream.xlsx.WorkbookWriter(options)
let sheet = workbook.addWorksheet('myWorksheet')
// add some data to the file
sheet.commit()
workbook.commit()
我现在想更改它以便将其上传到 Azure Blob 存储。 ExcelJS 允许指定 stream
而不是要提交的 filename
。所以我决定使用 Memory Streams JS 所以现在选项看起来像这样。
let stream = new streams.WritableStream()
let options = {
stream: stream,
useStyles: true,
useSharedStrings: true
}
通过阅读 Azure 文档,两种最合适的方法似乎是 createBlockBlobFromStream()
和 createWriteStreamToBlockBlob()
,它们似乎都需要可读流(如果我错了请纠正我)。这是我卡住的地方,因为我有一个使用 ExcelJS 的可写流,但我需要一个用于 Azure Blob 存储的可读流。显然我仍然可以将文件写入磁盘;从文件创建一个可读流;然后在上传到 Azure Blob 存储后最终删除该文件,但这会产生很大的开销。我是不是在用一种非常迂回的方式来解决这个问题?
Streaming XLSX Writer部分有说明:
If neither stream nor filename is specified in the options, the workbook writer will create a StreamBuf object that will store the contents of the XLSX workbook in memory. This StreamBuf object, which can be accessed via the property workbook.stream, can be used to either access the bytes directly by stream.read() or to pipe the contents to another stream.
因此您可以尝试以下代码片段,而无需在 options
中设置 filename
:
let blobSvc = azure.createBlobService();
let options = {
useStyles: true,
useSharedStrings: true
}
let workbook = new Excel.stream.xlsx.WorkbookWriter(options)
let sheet = workbook.addWorksheet('myWorksheet')
sheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 32 },
{ header: 'D.O.B.', key: 'DOB', width: 10, outlineLevel: 1 }
];
sheet.commit()
workbook.commit()
workbook.stream.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer','test.xlsx',function(err,result){
if(err)console.log(err)
console.log(result);
}))