【发布时间】:2020-04-05 05:20:58
【问题描述】:
我正在尝试读取 Firebase 函数中的 csv 文件,以便将邮件发送到所有记录。我打算按照以下程序进行
- 上传 csv 文件
- 触发终结函数
- 阅读文件并发送电子邮件
下面是函数
import * as functions from "firebase-functions";
import * as mkdirp from "mkdirp-promise";
import * as os from "os";
import * as path from "path";
import csv = require('csvtojson');
const gcs = require('@google-cloud/storage')({ keyFilename: 'service-account-credentials.json' });
const csvDirectory = "csv";
export = functions.storage.object().onFinalize(async (object) => {
const filePath = object.name;
const contentType = object.contentType;
const fileDir = path.dirname(filePath);
if(fileDir.startsWith(csvDirectory) && contentType.startsWith("text/csv")) {
const bucket = gcs.bucket(object.bucket);
const file = bucket.file(filePath);
const fileName = path.basename(filePath);
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
console.log("values", bucket, file, fileName, tempLocalDir, tempLocalFile);
console.log("csv file uploadedeeeed");
await mkdirp(tempLocalDir);
await bucket.file(filePath).download({
destination: tempLocalFile
});
console.log('The file has been downloaded to', tempLocalFile);
csv()
.fromFile(tempLocalFile)
.then((jsonObj) => {
console.log(jsonObj);
})
}
});
在运行代码时,我只上传了我在 console.log 中编写的 csv 文件,然后 1 分钟后我得到了超时。我也没有得到文件已下载到日志。任何人都可以查看代码并帮助我摆脱困境。
【问题讨论】:
-
你的文件上传到bucket成功了吗?
-
@BadPiggie 是的,我正在从 Firebase 存储中手动上传它
-
您能否展示您的整个
index.js文件,而不仅仅是 Cloud Function 的 sn-p。请同时添加导入。 -
@RenaudTarnec 立即查看
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions