Open klovehomes opened 5 years ago
These are the things I had to change to get it to deploy correctly:
- remove ( ) from require('google-cloud/storage')( ) line
- object().onChange() had to change to onFinalize( )
- and add a line ggg = new gcs( ) ; FINAL CODE SHOWN BELOW: const functions = require("firebase-functions"); const gcs = require('@google-cloud/storage'); const os = require('os'); const path = require('path'); const spawn = require('child-process-promise').spawn;
// // Create and Deploy Your First Cloud Functions // // https://firebase.google.com/docs/functions/write-firebase-functions // exports.onFileChange= functions.storage.object().onFinalize(event => { const object = event.data; const bucket = object.bucket; const contentType = object.contentType; const filePath = object.name; console.log('File change detected, function execution started');
if (object.resourceState === 'not_exists') { console.log('We deleted a file, exit...'); return; } if (path.basename(filePath).startsWith('resized-')) { console.log('THIS FILE HAS BEEN RENAMED ALREADY '); return; }
const ggg = new gcs() ; const destBucket = ggg.bucket(bucket); const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath)); const metadata = { contentType: contentType }; return destBucket.file(filePath).download({ destination: tmpFilePath }).then(() => { return spawn('convert', [tmpFilePath, '-resize', '500x500', tmpFilePath]); }).then(() => { return destBucket.upload(tmpFilePath, { destination: 'resized-' + path.basename(filePath), metadata: metadata }) }); });
I am able to deploy, but the function is still throwing error while execution.
TypeError: Cannot read property 'bucket' of undefined at exports.onFileChange.functions.storage.object.onFinalize.event (/user_code/index.js:12:26) at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) at /var/tmp/worker/worker.js:779:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Any tips to solve this issue ?
const functions = require("firebase-functions"); const { Storage } = require("@google-cloud/storage"); const projectId = "YOUR PROJECT ID"; let gcs = new Storage({ projectId, }); const os = require("os"); const path = require("path"); const spawn = require("child-process-promise").spawn;
// // Create and Deploy Your First Cloud Functions // // https://firebase.google.com/docs/functions/write-firebase-functions // exports.onFileChange = functions.storage.object().onFinalize((event) => { const bucket = event.bucket; const contentType = event.contentType; const filePath = event.name;
console.log("File change detected, function execution started");
if (event.resourceState === "not_exists") { console.log("We deleted a file, exit..."); return; }
if (path.basename(filePath).startsWith("resized-")) { console.log("We already renamed that file!"); return; }
const destBucket = gcs.bucket(bucket); const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath)); const metadata = { contentType: contentType }; return destBucket .file(filePath) .download({ destination: tmpFilePath, }) .then(() => { return spawn("convert", [tmpFilePath, "-resize", "500x500", tmpFilePath]); }) .then(() => { return destBucket.upload(tmpFilePath, { destination: "resized-" + path.basename(filePath), metadata: metadata, }); }); });
These are the things I had to change to get it to deploy correctly: 1) remove ( ) from require('google-cloud/storage')( ) line 2) object().onChange() had to change to onFinalize( ) 3) and add a line ggg = new gcs( ) ;
FINAL CODE SHOWN BELOW: const functions = require("firebase-functions"); const gcs = require('@google-cloud/storage'); const os = require('os'); const path = require('path'); const spawn = require('child-process-promise').spawn;
// // Create and Deploy Your First Cloud Functions // // https://firebase.google.com/docs/functions/write-firebase-functions // exports.onFileChange= functions.storage.object().onFinalize(event => { const object = event.data; const bucket = object.bucket; const contentType = object.contentType; const filePath = object.name; console.log('File change detected, function execution started');
const ggg = new gcs() ; const destBucket = ggg.bucket(bucket); const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath)); const metadata = { contentType: contentType }; return destBucket.file(filePath).download({ destination: tmpFilePath }).then(() => { return spawn('convert', [tmpFilePath, '-resize', '500x500', tmpFilePath]); }).then(() => { return destBucket.upload(tmpFilePath, { destination: 'resized-' + path.basename(filePath), metadata: metadata }) }); });