I have made a to-do list app using Firebase Auth and Firestore, and I would like to add an attach image feature, with which you can add images to tasks. I need to use a combination of Auth, Storage and Firestore but I'm not sure how to combine them.
How can I reach data from Firebase Storage using Firestore? Before I got into this, I was imagining it would be something like adding an imageLink field to each task, and then linking to the relevant image in Firebase Storage. Is it something similar? What should I do?
CodePudding user response:
Firestore and Cloud Storage are two different services offered by Firebase. They don't communicate with each other. It's up to you to make the link between the different "entities" stored in this two services.
A very common approach for your Use Case is to first upload the file in Cloud Storage, and then save in a Firestore document, either the path of the file or a signed URL to the file (or both, knowing that with the path you can always generate a signed URL).
Another approach consists in getting a Firestore document ID (automatically generated with the addDoc()
or setDoc()
methods) and use this ID to rename the file before saving it to Cloud Storage. This way the link between the file and Firestore is clearly materialized and you can very easily link them in your front end. You could also use this approach to put in a given folder all the files linked to one Firestore doc: you name the folder with the Document ID.
A third approach is to use the metadata of the Cloud Storage file to store the ID of a Firestore document. Most of the time this approach is less "handy": You either need to know the path of a file or you need to list all the files in a bucket or folder in order to get the ID of the Firestore docs. But for some specific cases it can be an interesting approach, knowing that nothing prevents you to combine approach #1 or #2 with approach #3, if necessary...