I am trying to import five JSON files from Firebase Storage to the Database on the 'onFinalize ()' event after loading them into Storage 'one by one' almost simultaneously. Each file must be uploaded to its own separate collection.
JSON files: usually these are 5 files (~ 13Mb ~ 9000 records, ~ 19Mb ~ 21000 records, ~ 23Mb 57000 records, ~ 95Mb 73000 records, and ~ 435Mb 208000 records) the total weight of all files ~ 600Mb
The code works on small test files (1Kb), but when I try to download at least 13Mb, I get errors:
- Unhandled rejection
- Error: 4 DEADLINE_EXCEEDED: Deadline Exceeded at Object.exports. : 28) at InterceptingListener._callNext (/srv/node_modules/grpc/src/client_interceptors.js.568:42:42) at InterceptingListener.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptremntr/src/client_interceptrem/svv/node_modules/grcc/src/client_interceptrns / node_modules/grcc/client_interference.js68/4/4/4/2/5/5/5/5/5/5/5/2/Erc/client_interviews/srv/node_modules/grpcLs_callNext /srv/node_modules/grpc/src/client_interceptors.js:845:24)
and only the first object from the file is added to the collection, and then it looks like the function went into recursion :(
const functions = require('firebase-functions') const { Storage } = require('@google-cloud/storage') const storage = new Storage() const admin = require('firebase-admin') admin.initializeApp(functions.config().firebase) const db = admin.firestore() const path = require('path') exports.logNewJSONFiles = functions .runWith({ timeoutSeconds: 120, memory: '2GB' }) .storage.object() .onFinalize((file) => { return new Promise(function(resolve, reject){ storage .bucket(file.bucket) .file(file.name) .download() .then(function(data){ if (data) return JSON.parse(data) }) .then(function(data){ if (data) { const destination = db.collection(path.basename(file.name)) for (var obj of data) destination.doc().set(obj) } return resolve() }) .catch(function(e){ reject(e) }) }) })