如何在 Node.js 中将 CSV 文件从 Cloud Functions 上传到 Cloud Storage?

编程入门 行业动态 更新时间:2024-10-05 01:21:08

如何在 Node.js <a href=https://www.elefans.com/category/jswz/34/1771445.html style=中将 CSV 文件从 Cloud Functions 上传到 Cloud Storage?"/>

如何在 Node.js 中将 CSV 文件从 Cloud Functions 上传到 Cloud Storage?

我正在尝试将从 Cloud Function 中的对象创建的 CSV 文件上传到 Cloud Storage 存储桶。我目前正在使用 @google-cloud/storage 库来处理上传。 CSV 文件保存在 Cloud Function 实例的 tmp 文件夹中。

这是我目前的实现



const saveCsv = async () => {
  const { Storage } = require('@google-cloud/storage');
  const createCsvWriter = require('csv-writer').createObjectCsvWriter;

  const data = [
    {
      name: 'John',
      surname: 'Doe',
      age: 35,
      gender: 'M',
      city: 'New York',
    },
    {
      name: 'Jane',
      surname: 'Doe',
      age: 28,
      gender: 'F',
      city: 'Los Angeles',
    },
  ];

  const csvPath = '/tmp/output.csv';
  const csvWriter = createCsvWriter({
    path: csvPath,
    header: [
      { id: 'orderNumber', title: 'orderNumber' },
      { id: 'zipcode', title: 'zipcode' },
      { id: 'address', title: 'address' },
      { id: 'phoneNumber', title: 'phoneNumber' },
      { id: 'name', title: 'name' },
    ],
  });
  csvWriter.writeRecords(data);

  const storage = new Storage();
  const bucketName = 'order_numbers';

  storage.bucket(bucketName).upload(csvPath);
};


exports.uploadToDrive = functions
  .https.onRequest(async (req, res) => {
    await save(todayIntentData);
});

结果

Function execution took 60001 ms, finished with status: 'error'

回答如下:

更多推荐

如何在 Node.js 中将 CSV 文件从 Cloud Functions 上传到 Cloud Storage?

本文发布于:2024-05-30 19:40:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   文件   如何在   js   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!