Aimn Blbol
Aimn Blbol

Reputation: 1285

Retrieve image stored in Firebase storage

I am using Dart / Flutter. I would like to know what is the best practice when retrieving images stored into Firebase Storage. I can see two possible ways but I am not sure which one is better.

First approach:

After uploading the image into Firebase Storage, I save the url of the image into Firestore and use this url to retrieve the image when I need. I can see one problem with this approach. Each image url have access token associated with it which can be revoked. and if it is revoke, the url stored in Firebase no longer works. Also, I am not clear on how long can an access token live. Does anybody know?

Second approach:

Don't save the url of the image in Firestore but rather get it every time you need to view the image. The problem with this approach is that you will have to send two request just to see one image. First request to get the url, and the second request to see the image. What if I have a grid of 9 images that I need to show in one page? That is 18 rerquest just for viewing images.

I can see a third way which is to download the image into local disk but I could not find a good library to help with that since you would not want you loca cache to keep growing infinitely.

Could someone be kind and set me in the right direction? Thanks in advance.

Upvotes: 1

Views: 610

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

Access tokens for Firebase Storage download URLs don't expire. If a token becomes invalid, it's because you revoked it (from the Firebase console) - in which case I assume you actually want the user to lose access to that image.

Saving download URLs is the idiomatic way to solve this use-case, where users need public access.


In your second approach you could consider download the data through the SDK, instead of through a download URL. That would lead to a single request per image.


Also see:

Upvotes: 2

Related Questions