Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Had an issue getting this to work as posted #2

Open
klovehomes opened this issue Feb 3, 2019 · 2 comments
Open

Had an issue getting this to work as posted #2

klovehomes opened this issue Feb 3, 2019 · 2 comments

Comments

@klovehomes
Copy link

klovehomes commented Feb 3, 2019

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants