Search code examples
firebasegoogle-cloud-functionsgoogle-cloud-storageemulationfirebase-tools

Firebase onFinalize doesn't trigger for functions emulator


I'm setting up the env for local development and just testing to see if the emulators are working but my simple test is failing without any errors.

// functions/index.js
exports.pdftoimg = functions.storage.object().onFinalize(async (object) => {
  console.log({ object });
});

Emulators start without issues:

firebase emulators:start
i  emulators: Starting emulators: auth, functions, firestore, hosting, pubsub, storage
✔  functions: Using node@16 from host.
i  hosting: Serving hosting files from: dist
✔  hosting: Local server: http://localhost:5002
i  functions: Watching "/Users/xxx/Code/myApp/functions" for Cloud Functions...
✔  functions[us-central1-pdftoimg]: storage function initialized.

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

But when I upload a test pdf file to storage emulator using the UI (http://localhost:4000/storage/default-bucket) nothing is displayed in the logs.

I tested the cloud triggers for Firestore Db but that seems to work though.

What am I missing?

Thanks


Solution

  • I can see you are Triggering a Cloud Function on Cloud Storage Changes that returns the uploaded object metadata.

    However, this function does not upload the file:

    onFinalize Sent when a new object (or a new generation of an existing object) is successfully created in the bucket. This includes copying or rewriting an existing object. A failed upload does not trigger this event.

    In that case please follow the documentation provided to Upload Files. Finally, adding Error Handling to your code will help you the next time as in this case you have no logs or anything to debug the error.

    Update:

    This issue onFinalize trigger not working in emulator, that uses the same Cloud Functions with the same results. We have to wait for support on that.

    After some research a colleague found this github issue that uses the same documentation and the same methods you are using. The first one describes how ‘onFinalize’ is triggered even on failed upload: and also redirects to the documentation that was mentioned above. In this case the function is triggered when it’s not supposed to, you can try executing it like this.