academind / firebase-cloud-functions-introduction

A miniseries introducing Firebase Cloud Functions (Also see: https://www.academind.com/learn/firebase/cloud-functions)
58 stars 35 forks source link

Had an issue getting this to work as posted #2

Open klovehomes opened 5 years ago

klovehomes commented 5 years ago

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');

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 }) }); });

alihabib-in commented 5 years ago

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');

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 ?

kbarejko commented 2 years ago

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, }); }); });