Functions-samples: Deploy Error: Failed to configure trigger GCS Bucket

Created on 20 Mar 2017  Â·  21Comments  Â·  Source: firebase/functions-samples

Hi, I'm trying out the image resizing demo. Deployment throwing error:

 Deploy Error: Failed to configure trigger GCS Bucket: product_images

My images are inside product_images folder and my code looks like this:


exports.generateThumbnail = functions.storage.object().onChange(event => {
  const object = event.data;
  const fileBucket = object.bucket;
  const filePath = object.name;
  const contentType = object.contentType;
  const resourceState = object.resourceState;

  if(!filePath.match(/product_images/)) {
    console.log('not product_images bucket');
    return;
  }

  if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return;
  }

  const fileName = filePath.split('/').pop();
  // Exit if the image is already a thumbnail.
  if (fileName.startsWith('thumb_')) {
    console.log('Already a Thumbnail.');
    return;
  }

  if (resourceState === 'not_exists') {
    console.log('This is a deletion event.');
    return;
  }

  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = `/tmp/${fileName}`;

  return bucket.file(filePath).download({
    destination: tempFilePath
  }).then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '64x64>', tempFilePath]).then(() => {
      console.log('Thumbnail created at', tempFilePath);
      // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
      const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`);
      // Uploading the thumbnail.
      return bucket.upload(tempFilePath, {
        destination: thumbFilePath
      });
    });
  });
});

Not sure what it means.

bug

Most helpful comment

I was having the same problem.
Apparently it is caused by missing permission on service account executing cloud function.

I just added Storage Object Admin role to default to default service account and everything start to work as expected.
image

All 21 comments

Can you tell us a bit more about when the deploy error happens? Is that when you are running the CLI firebase deploy? Can you copy paste the full output off the CLI?

Exactly, during deployment process. I've searching more about this, and it seems we need to have billing enabled and firebase store and google cloud store are two different products. My understanding was that the image resizing function works on firebase store which apparently is not correct. My use case is, when someone uploads file to firebase store (using firebase client) I want to resize image using cloud function. Could you please clarify if that's possible?

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
✔  runtimeconfig: all necessary APIs are enabled
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (2.63 KB) for uploading
✔  functions: functions folder uploaded successfully
i  starting release process (may take several minutes)...
i  functions: updating function addTag...
i  functions: updating function adjustments...
i  functions: updating function generateThumbnail...
âš   functions[generateThumbnail]: Deploy Error: Failed to configure trigger GCS Bucket: product_images
✔  functions[addTag]: Successful update operation.
✔  functions[adjustments]: Successful update operation.
✔  functions: 2 function(s) deployed successfully.

Can you change:

exports.generateThumbnail = functions.storage.bucket().object().onChange(event => {

to:

exports.generateThumbnail = functions.storage.object().onChange(event => {

?

It looks like Functions thinks you want a trigger on the bucket named product_images which would happened if your code was:

exports.generateThumbnail = functions.storage.bucket('product_images').object().onChange(event => {

Even if that works, could you paste what you have in your code above so we can investigate?

This is what I have, same as you suggested.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage')();
const spawn = require('child-process-promise').spawn;

exports.generateThumbnail = functions.storage.object().onChange(event => {
  const object = event.data;
  const fileBucket = object.bucket;
  const filePath = object.name;
  const contentType = object.contentType;
  const resourceState = object.resourceState;

  if(!filePath.match(/product_images/)) {
    console.log('not product_images bucket');
    return;
  }

  if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return;
  }

  const fileName = filePath.split('/').pop();
  // Exit if the image is already a thumbnail.
  if (fileName.startsWith('thumb_')) {
    console.log('Already a Thumbnail.');
    return;
  }

  if (resourceState === 'not_exists') {
    console.log('This is a deletion event.');
    return;
  }

  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = `/tmp/${fileName}`;

  return bucket.file(filePath).download({
    destination: tempFilePath
  }).then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '64x64>', tempFilePath]).then(() => {
      console.log('Thumbnail created at', tempFilePath);
      // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
      const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`);
      // Uploading the thumbnail.
      return bucket.upload(tempFilePath, {
        destination: thumbFilePath
      });
    });
  });
});

This looks like it's a backend issue. I can investigate; do we have permission to view logs related to your account?

@inlined Sure! Let me know if anything is required.

I found a stack trace in our backend; since this conversation includes details about your personal app I'm going to reach out to you at the email address listed on your GitHub profile.

For posterity: Cloud Functions requires editor permission on your project.

FYI this issue is somewhat widespread because we used to have a bug (as of 1-2 month ago) where lots of Cloud Storage Buckets were created with the wrong permissions. New projects don't have this issue but if you have an old Firebase project which is impacted by this the manual fix for your permissions is described here:

https://github.com/firebase/friendlychat/issues/184#issuecomment-290914672

Yes @inlined mailed me with details. I'm guessing the particular project I was working on was affected by the bug. It worked fine with a new project.

Glak you got it sorted @amitava82 !

I'm changing the title and added the comment above so that it's easier to find for other developers that might be infected.

Just had this happen with a Realtime Database trigger for firebase. Should I report as bug? Trigger has not been edited and has worked previously.

Please contact support directly. GitHub bugs are for bugs with the SDK.

@inlined I am getting this exact same issue. Would you mind having a look for me?

https://stackoverflow.com/questions/48901888/firebase-storage-trigger-not-working

I'm having the same issue.

I'm deploying a function and getting Failed to configure trigger GCS Bucket.

I'm declaring the function as required per the docs:

functions.storage.object().onFinalize((object) => {...})

This exact same function deploys ok on another project.

Edit:
I've created a new Firebase project and tried to deploy with the same error.

I've contacted Firebase support but I need to solve this ASAP and these guys are usually quite slow.

I was having the same problem.
Apparently it is caused by missing permission on service account executing cloud function.

I just added Storage Object Admin role to default to default service account and everything start to work as expected.
image

I have added permission but still not working for me

I started receiving these messages as well with a newly created firebase project. It was working previously with my old project.

Same for me. I've tried everything I could find online (changing IAM, adding roles, updating permissions...) but the deploy won't work. Even with a new project. My other project (created a year ago) works fine though...

@peterwarbo I haven't changed a thing and now it works for me. Could you try again ?

@mparpaillon ah, will try again. Maybe it was a temporary outage with storage 😊

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MalikFaizanZafar picture MalikFaizanZafar  Â·  4Comments

Qzingo picture Qzingo  Â·  3Comments

IchordeDionysos picture IchordeDionysos  Â·  3Comments

phuduong060196 picture phuduong060196  Â·  4Comments

beratuslu picture beratuslu  Â·  4Comments