Is it possible to create one Firebase Storage trigger and apply it on all buckets? [duplicate] - firebase

This question already has answers here:
Is it possible to include multiple buckets as --trigger-resource for a google cloud function?
(2 answers)
Closed 5 days ago.
I have a Firebase Project that has many buckets
I want to implement a trigger onFinalize() and onDelete(), this trigger will have the same logic for all buckets in the storage
Can't I use this trigger on all buckets? or do I need to create a new specific trigger for each bucket separately?

It's not possible to apply a single deployed trigger to multiple buckets. Each bucket must have its own trigger deployed (but you are of course free to share the same code for each one). See the documentation:
Use functions.storage to create a function that handles Cloud Storage
events. Depending on whether you want to scope your function to a
specific Cloud Storage bucket or use the default bucket, use one of
the following:
functions.storage.object() to listen for object changes on the default Cloud Storage bucket.
functions.storage.bucket('bucketName').object() to listen for object changes on a specific bucket.

Related

Accessing user authentication information within a Cloud Firestore triggered Cloud Function [duplicate]

This question already has answers here:
Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?
(9 answers)
Does the firebase cloud functions context.auth field work for firestore? [duplicate]
(1 answer)
Closed last month.
I need to get signed in user id when a Cloud Function listening to change in db is triggered
I tried firestore.getAuth() without success.
I need to get signed in user id when a Cloud Function listening to
changes in db is triggered
It is not possible with Cloud Functions for Firestore to access the details of the authenticated user who did the modification that triggered the CF. One workaround is to write the user ID in a field in the Firestore document.
FYI note that, in the other hand, this is possible with the Realtime Database.

Firebase authentication aggregate data [duplicate]

This question already has an answer here:
Add additional data to user profile via firestore functions onCreate
(1 answer)
Closed 2 years ago.
I have two different applications; let's call them A and B. I use Firestore to store additional user data under a collection called /users.
I have a cloud function that aggregates data whenever a user registers through Google, manual or Facebook to the users collection.
functions.auth.user().onCreate( (user, ctx) => {
//aggregate data in /users
});
The problem is that I want to add another collection, say different-users that are only added when using application B. According to Firebase documentation:
You cannot add other properties to the user object directly; instead,
you can store the additional properties in any other storage services,
like Google Cloud Firestore.
How am I supposed to differentiate these types of users in my cloud function giving the fact that I can't add additional data?
Am I missing something?
The user is created in Firebase Authentication and that is what triggers your Cloud Function. At the point your backend code gets invoked, there is no information anymore about what app called the authentication API.
If you want to store additional information based on where the user was created from, consider calling a Callable Cloud Function directly from within your application code after the creation of the user in Firebase Authentication completes. Alternatively, you could write the info directly to the database from the client, if that works for your use-case.
Also see:
Add additional data to user profile via firestore functions onCreate (which I now see is a duplicate, so I'll close against it in a minute)
Firebase Cloud Function - Create user with extra information

I want to automatically update a child value in firebase realtime database monthly [duplicate]

This question already has answers here:
Cloud Functions for Firebase trigger on time?
(3 answers)
Closed 4 years ago.
I want to automatically update a child value in firebase database, monthly.. How can I handle this?
There is nothing built into the Firebase Realtime Database to automatically update values periodically. So you will have to write the code for this yourself.
After you write the code you will need to run that code periodically.
If you put the code into the app, you can run it periodically there. It's technically possibly, but a bit involved (is the app running all the time?, what if multiple users run it?, etc).
So the more common approach is to run that code on a server. If you don't have a server yet, you can run the code in Cloud Functions for Firebase, which allows you to run small pieces of (for now only JavaScript or TypeScript) code on Google's servers. To trigger this code to run monthly, see Cloud Functions for Firebase trigger on time?
There is nothing fully integrated at the moment.
One way would be to use a Google Cloud scheduler job that will start your process in a Google Cloud function once a month.
You will only pay once a month for the duration of your function.
You can have an example here

Database Triggered Cloud Functions across multiple Firebase projects

Is it possible to trigger a cloud function on a database instance of another firebase project? From what I've read so far, the documents mention passing an instance name in the function but this would be an instance in the same project.
It's not possible. Both functions and database shards must be contained within a single project.

Periodically delete data from firebase in certain time [duplicate]

This question already has answers here:
Delete firebase data older than 2 hours
(5 answers)
Closed 2 years ago.
I want to clear data from Firebase database every 24 hours, but I do not want to do this from code in my project, but from the Firebase.
Do firebase have such functions?
Yes it does. Please use the functions from Cloud Functions for Firebase.
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.
Hope it helps.

Resources