【问题标题】:Azure Function how to convert JSON to Excel and save it to blob storageAzure Function 如何将 JSON 转换为 Excel 并将其保存到 blob 存储
【发布时间】:2021-02-03 10:16:17
【问题描述】:

我想知道,在运行此 js 代码时,如何将生成的 Excel 文件保存到 blob 存储中。请问,您知道如何执行类似 fs.safeFileSync 的东西吗?

const fs = require('fs');
var json2xls = require('json2xls');
var json = {
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: "tester"
}

var pole = [];

pole.push(json);
pole.push(json);
pole.push(json);
pole.push(json);

var xls = json2xls(pole);

console.log("done, saved", xls);
fs.writeFileSync('data.xlsx', xls, 'binary'); //this saves file to disk, but I need to save it to a blob storage with the function below
context.bindings.outputBlob = ??????

【问题讨论】:

  • 您是否尝试过将xls 变量传递给context.bindings.outputBlob
  • 是的,我有。它确实通过了文件,但它说,文件已损坏

标签: javascript node.js azure-functions azure-blob-storage


【解决方案1】:

如果有人感兴趣,我是这样解决的:

const csval = require("csval");
var json2xls = require('json2xls');
var fs = require('fs');
const { BlobServiceClient } = require('@azure/storage-blob');
const AZURE_STORAGE_CONNECTION_STRING = process.env.storageCred;

module.exports = async function (context, myQueueItem) {

    context.log('JavaScript queue trigger function processed work item', myQueueItem);
    const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

    var json = {
        foo: 'bar',
        qux: 'moo',
        poo: 123,
        stux: new Date()
    }


    var arry = [];
    arry.push(json);
    arry.push(json);

    var xls = json2xls(arry); 
    const data = Buffer.from(xls,'binary')
    const blobName = "excelfile.xls";
 

    const containerClient = blobServiceClient.getContainerClient("incontainer");
    const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    const uploadBlobResponse = await blockBlobClient.upload(data, data.length);

    context.log("done")
};

【讨论】:

    猜你喜欢
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2021-10-20
    • 2020-01-23
    • 2021-11-01
    • 2020-08-25
    • 2020-11-09
    相关资源
    最近更新 更多