Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a reference to a Cloud Storage document in Firestore

I am using Firebase and I try to create a reference to a Cloud Storage document in a Firestore document.

In my Console > Storage, I have a folder discovery who contain a file heart.png.

In my Firestore document, I have a reference field named src. I have tried to fill it with:

  • discovery/heart.png
  • gs://<NAME>.appspot.com/discovery/heart.png
  • <NAME>.appspot.com/discovery/heart.png

but it is doesn't work.. When I try to get the image like this src.get().then(snap => console.log(snap.data())) the result is always undefined.

Does the reference fields are only designed to the databases documents (not Storage ones) ? I can't find how to reference a Storage file in a Firestore document neither on the documentation nor on Google.

Could someone help me please ? :) Thanks a lot !

like image 777
Quentin Brosse Avatar asked Dec 19 '17 12:12

Quentin Brosse


People also ask

How to Create a reference in Firebase storage?

References are lightweight, so you can create as many as you need, and they are also reusable for multiple operations. To create a reference, get an instance of the Storage service using getStorage() then call ref() with the service as an argument. This reference points to the root of your Cloud Storage bucket.

What is a reference in firestore?

firestore. DocumentReference. < T > A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist.

How can I get URL of uploaded image in Firebase storage?

Image URL is obtained by uploading an image to firebase bucket and then that can return back a URL that URL is a permanent URL which can be open anywhere. Then a user can use this URL for any purpose in its application.


1 Answers

The Reference field type of Cloud Firestore can only refer to another document in the Firestore database.

If you want to refer to an object/location in Cloud Storage, you'll have to store that as a compatible type. For example by storing the download URL of the object, or the path to the object.

like image 126
Frank van Puffelen Avatar answered Nov 15 '22 19:11

Frank van Puffelen