Firebase Cloud Storage download url vs path
Asked Answered
D

3

9

I'm new to firebase storage and wants to know what is the best practice. I want to upload image to firebase cloud storage and was returned a download url which I then stored to firestore. Is the download url permanent? Other users will read from the firestore to get the url to download the image.

But when I want to delete the image from CloudStorage, I only have the download url but not the file path. So do I delete it ?

If I store the file path instead, how to get the download url ?

Duong answered 10/9, 2018 at 6:55 Comment(0)
M
11

Is the download url permanent?

The download URL will work until you revoke it.

I only have the download url but not the file path. So do I delete it?

You can get a StorageReference from a download URL by calling FirebaseStorage.getReferenceFromUrl() (or the equivalent for your platform).

If I store the file path instead, how to get the download url?

You can create a StorageReference for the path with FirebaseStorage.getReference(), and then call StorageReference.getDownloadUrl().

Mendelian answered 10/9, 2018 at 14:43 Comment(1)
Thanks. I was using flutter and found that the function was not available at the moment. Anyway thanks for your answer. Will just have to save both the storage path and url for now until it is supported.Duong
M
1

I spent my day refactoring my entire project, shifting from saving the download URL to storing the reference path. Wondering why? Initially, I considered:

  1. Avoiding saving both because it felt redundant and costly.
  2. Thinking to save only the download URL is smart, beacause i need just this value in my application and if I ever needed the reference, I could just invoke the refFromUrl method.

However, this approach posed some issues:

  1. Currently (firebase_storage version 11.6.0), there's an issue with the refFromUrl method when using the emulator. Consequently, it is not possible to get the Image Reference during the testing phase, which is a significant problem.
  2. The Download Link is a public link, raising security concerns. Anyone can access the link and potentially request your image. Firestore lacks the ability to set a scaling/download limit, meaning if someone decides to request your image excessively, you end up incurring charges. Storing only the reference and fetching the download link on demand seems a safer option to me.
  3. The most significant point for me was speed, especially with Cloud Functions. In my case, I needed to edit an image stored in Python Cloud Functions and then write the result image back into storage. Using the download link in the Cloud Function to fetch the image took 3000ms, whereas loading the image directly from storage using the reference only required 600ms.

So, I transitioned my entire project to use only the storage reference. I can retrieve the download link in my application as needed with getDownloadUrl(), eliminating the necessity to save both and saving memory. Additionally, updating a single value is simpler than managing two. These were my considerations when I switched from the download URL to the storage reference. I hope this provides some insights for you! :)

My Recommendation:

Instead of using .getDownloadUrl(), it's better to use .getData() directly.

For example, when displaying images, you might be tempted to use NetworkImage(downloadUrl) because it seems straightforward at first glance. However, this approach creates a long-lived URL, which can pose security risks. Additionally, if you only store the image reference, you'll have to await the URL every time, resulting in two calls for each image.

A more efficient and secure alternative is to use the getData() method. This method retrieves the data directly from the reference and also checks the security rules.

Maltese answered 10/1, 2024 at 16:8 Comment(0)
K
0

I know it's an old post but this is what I use in Flutter:

final Reference storageReferenceFromUrl = FirebaseStorage.instance.refFromURL(downloadPhotoPath!);
storageReferenceFromUrl.delete();

Hopefully, this helps.

Karyn answered 6/2, 2023 at 20:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.