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.
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.
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 StorageIn 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.
We successfully deployed our function to Firebase. We will open the function’s logs by clicking on View logs
in the ellipsis menu:
Apart from that we will upload something to Storage:
We uploaded an image:
If you check the function’s logs you can see information about the latest uploaded file:
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.