0

I need to store some images inside Firebase Storage and I was wondering if there's any way to add a record inside Realtime DB with the download url when adding an image inside Storage.

I don't want to do it necessarily from an app, is there a way to add a record to DB just by adding an image inside Storage from the console?

I was trying to retrieve the images urls directly from Storage but in some cases it resulted tedious and I thought that putting the urls inside the DB would be easier. And it was a good opportunity to try Realtime DB too.

alburt
  • 11
  • 6
  • You can always copy the URL from Firebase storage console and paste in RTDB but that'll be manual process. What are you looking for? or you can use [Cloud Storage Triggers](https://cloud.google.com/functions/docs/calling/storage) and use a cloud function to update database once image is uploaded – Dharmaraj Oct 28 '22 at 09:44
  • Do you need to do that programmatically or manually? If you understand Kotlin, this [resource](https://medium.com/firebase-tips-tricks/how-to-upload-an-image-to-cloud-storage-and-save-the-url-in-firestore-42711ca1df46) will definitely help. Please respond using @AlexMamo – Alex Mamo Oct 28 '22 at 09:48
  • @Dharmaraj i want it to be automatic, i'll take a look on the triggers, i think that could be the answer – alburt Oct 28 '22 at 09:51
  • @AlexMamo i want it to be automatic and i'm more experienced in JS :) – alburt Oct 28 '22 at 09:52
  • And you need to code for that? – Alex Mamo Oct 28 '22 at 09:54
  • @AlexMamo well yes, i'm working on a project for this – alburt Oct 28 '22 at 10:02

1 Answers1

0

You can use Cloud Storage Triggers for Cloud Functions that'll add the URL in database after a file is uploaded like this:

exports.updateDb = functions.storage.object().onFinalize(async (object) => {
  // read object data
  // add required data to Firebase RTDB
});

You cannot get the Firebase storage download URLs with the ?token= parameter using Admin SDK so also checkout How can I generate access token to file uploaded to firebase storage?.

If you don't want to use Cloud Functions then you can simply fetch the download URL, and then update database directly from client SDK.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84