I am trying to use a Cloud-Function to export a Collection in from Cloud-Storage.
I followed this blog and deployed the following function:
const functions = require("firebase-functions");
const firebase = require("firebase-admin");
const { parseAsync } = require('json2csv');
const { v4: uuidv4 } = require('uuid');
const fs = require("fs");
const path = require("path");
const os = require("os");
const generateHostsCsv = functions.region("europe-west1").pubsub
.topic("generate-hosts-csv")
.onPublish(async message => {
// gets the documents from the firestore collection
const hostsSnapshot = await firebase
.firestore()
.collection("data")
.doc("analytics")
.collection("hosts")
.get();
const hosts = hostsSnapshot.docs.map(doc => doc.data());
// csv field headers
const fields = [
"hostName",
"count",
];
// get csv output
const output = await parseAsync(hosts, { fields });
// generate filename
const dateTime = new Date().toISOString().replace(/\W/g, "");
const filename = `hosts_${dateTime}.csv`;
const tempLocalFile = path.join(os.tmpdir(), filename);
return new Promise((resolve, reject) => {
//write contents of csv into the temp file
fs.writeFile(tempLocalFile, output, error => {
if (error) {
reject(error);
return;
}
const bucket = firebase.storage().bucket();
// upload the file into the current firebase project default bucket
bucket
.upload(tempLocalFile, {
// Workaround: firebase console not generating token for files
// uploaded via Firebase Admin SDK
// https://github.com/firebase/firebase-admin-node/issues/694
metadata: {
metadata: {
firebaseStorageDownloadTokens: uuidv4(),
}
},
})
.then(() => resolve())
.catch(errorr => reject(errorr));
});
});
});
module.exports = { generateHostsCsv };
When I let it run, I only see this in my logs:
Function execution took 60000 ms, finished with status: 'timeout
But no file was created in my storage. What am I missing here? Let me know if you need more information.
As mentioned in the comments, it worked after I tried it for the third time.
Thanks @Doug for the help. I increased the timeout and also added some
logsso I can see what exactly is causing the issue.