-
Notifications
You must be signed in to change notification settings - Fork 35
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
Comments
I am able to deploy, but the function is still throwing error while execution. TypeError: Cannot read property 'bucket' of undefined Any tips to solve this issue ? |
const functions = require("firebase-functions"); // // Create and Deploy Your First Cloud Functions console.log("File change detected, function execution started"); if (event.resourceState === "not_exists") { if (path.basename(filePath).startsWith("resized-")) { const destBucket = gcs.bucket(bucket); |
These are the things I had to change to get it to deploy correctly:
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
})
});
});
The text was updated successfully, but these errors were encountered: