node.js - 将csv数据直接写入node js中的azure blob

标签 node.js azure csv azure-blob-storage

我想使用 csv-writer 创建 CSV 文件,并将该 csv 上传到 azure blob,我可以创建 csv,将其存储在本地系统上,然后使用 azure-storage 从本地读取并上传到 blob npm。但我不想在本地文件系统上创建/存储 CSV(因为我在 Prod 上遇到一些问题),有什么方法可以创建 CSV 并直接提供给 azure blob 存储,而不将 csv 写入本地文件系统。

一些代码供引用

const csvWriter = createCsvWriter({
        path: `__dirname/${blobName}`,
        header: [
          { id: "id", title: "name" },
        ],
      });

      await csvWriter
        .writeRecords(csvData)
        .then(() => console.log("file successfully written")); 

在本地创建此 csv 后,使用 fs 模块从那里读取它,并使用“blobService.createBlockBlobFromStream”函数上传到 blob。

您能否建议我如何直接将 azure blob 存储的路径提供给 csvWriter?或者还有其他方法可以实现这一目标吗?

最佳答案

请尝试下面的代码。

const {BlobServiceClient, StorageSharedKeyCredential} = require('@azure/storage-blob');
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;
const accountName = 'account-name';
const accountKey = 'account-key';
const container = 'container-name';
const blobName = 'text.csv';

const csvStringifier = createCsvStringifier({
    header: [
        {id: 'name', title: 'NAME'},
        {id: 'lang', title: 'LANGUAGE'}
    ]
});
const records = [
    {name: 'Bob',  lang: 'French, English'},
    {name: 'Mary', lang: 'English'}
];
const headers = csvStringifier.getHeaderString();
const data = csvStringifier.stringifyRecords(records);
const blobData = `${headers}${data}`;
const credentials = new StorageSharedKeyCredential(accountName, accountKey);
const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`, credentials);
const containerClient = blobServiceClient.getContainerClient(container);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const options = {
    blobHTTPHeaders: {
        blobContentType: 'text/csv'
    }
};
blockBlobClient.uploadData(Buffer.from(blobData), options)
.then((result) => {
    console.log('blob uploaded successfully!');
    console.log(result);
})
.catch((error) => {
    console.log('failed to upload blob');
    console.log(error);
});

这段代码主要有两件事:

  1. 使用createObjectCsvStringifier如果您不想将数据写入磁盘。

  2. 使用@azure/storage-blob Node 包而不是 azure-storage 包,因为前者是较新的包,后者已被弃用。

<小时/>

更新

这是使用azure-storage包的代码。

const azure = require('azure-storage');
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;
const accountName = 'account-name';
const accountKey = 'account-key';
const container = 'container-name';
const blobName = 'text.csv';

const csvStringifier = createCsvStringifier({
    header: [
        {id: 'name', title: 'NAME'},
        {id: 'lang', title: 'LANGUAGE'}
    ]
});
const records = [
    {name: 'Bob',  lang: 'French, English'},
    {name: 'Mary', lang: 'English'}
];
const headers = csvStringifier.getHeaderString();
const data = csvStringifier.stringifyRecords(records);
const blobData = `${headers}${data}`;

const blobService = azure.createBlobService(accountName, accountKey);
const options = {
    contentSettings: {
        contentType: 'text/csv'
    }
}
blobService.createBlockBlobFromText(container, blobName, blobData, options, (error, response, result) => {
    if (error) {
        console.log('failed to upload blob');
        console.log(error);
    } else {
        console.log('blob uploaded successfully!');
        console.log(result);
    }
});

关于node.js - 将csv数据直接写入node js中的azure blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66653520/

相关文章:

node.js - 有什么方法可以在没有扩展名的情况下从 express 提供静态 html 文件?

node.js - 从集合中获取并迭代

Azure ARM 模板为数组参数中的每个值添加前缀

python - 在 Python 中匹配两个列表以从第三个列表中检索值

php - 使用 Laravel 将表格下载为 CSV

javascript - twilio 中的 statusCallback 未触发

javascript - 如何在nodejs控制台中隐藏密码?

Azure存储底层技术

c# - Azure AD 为 "external provider"?

python csv标题不在第一行