Firebase Functions Storage Trigger. How to Use "onFinalize"?


cloud firebase storage

In this post, I will show you the basic usage of storage triggers in the Firebase Cloud Functions. We will use .onFinalize as an example.

Introduction

For example, you need to do something with the newly uploaded file. It can be processing audio, generating the thumbnail, etc. For this purpose, we can create a function that will listen to Cloud Storage Events.

.onFinalize

We will deploy the basic function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

exports.storage = functions.storage.object().onFinalize(async (object) => {
  console.log(object)
});
  • functions.storage.object() - will listen object changes in the main bucket
  • .onFinalize - will fires when something will created on Firebase Storage

In simple words: when we upload something to Firebase Cloud Storage, .onFinalize will be sent and we can see information about the file in the logs.

Let’s deploy this function using the firebase deploy and go to the Firebase Console.

Testing

We successfully deployed our function to Firebase. We will open the function’s logs by clicking on View logs in the ellipsis menu:

Storage Cloud Function

Apart from that we will upload something to Storage:

firebase-storage-upload

We uploaded an image:

Uploaded image to Firebase Storage

If you check the function’s logs you can see information about the latest uploaded file:

Logs

Conclusion

In this post, we learned how to use .onFinalize to detect new files (or a new version of an existing file) in the Firebase Storage bucket.

comments powered by Disqus