Tat*_*dan 2 node.js firebase google-cloud-platform google-cloud-functions
我在 Node.js 中使用 Cloud Functions for Firebase,其中使用 excel4node lib 从对象导出 excel 文档,现在的问题是,在wb.write('Excel.xlsx');我必须将该 excel 文件实际保存到某处之后,然后再将其发送到用户可以下载的前端它。
我尝试做的事情:
我知道 Firebase 有 Cloud Storage,但由于一些奇怪的原因,只有 google 知道,根据这篇文章:TypeError: firebase.storage 不是一个函数Firebase Storage 不再与 Node.js 一起使用。
我还阅读了有关 Google Cloud 存储的信息,但这只是通过付费服务实现的。
这里有一个教程: https: //mashe.hawksey.info/2017/06/cloud-functions-for-firebase-in-google-apps-script/但同样,您需要有付费计划。
我的问题是:是否有任何免费的方法可以使用 firebase 和 Nodejs 进行导出,以便我能够将导出的 excel 保存在某处并将其输出到前端,以便从用户那里下载?
您在第 3 条教程中发布的链接仅需要付费计划,因为它会发出外部 HTTP 请求。如果您只是使用内部存储,您可以执行以下操作:
const path = require('path');
const os = require('os');
const fs = require('fs');
var bucket = admin.storage().bucket();
//cloud functions can only write to the temporary directory
const tempFilePath = path.join(os.tmpdir(), "Excel.xlsx");
//creates and writes the file to the local temporary directory
//and then retrieves the newly created file for uploading
wb.write(tempFilePath, function (err, stats) {
if (err) {
console.error(err);
} else {
console.log("File written.");
fs.readFile(tempFilePath, function (err, data) {
if (err) {
throw err;
}
var uploadTask = bucket.upload(tempFilePath,
{ destination: "excelFileFolder/Excel.xlsx"});
// Uploads the excel file to a folder called excelFileFolder in your firebase storage
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2516 次 |
| 最近记录: |