Pavel Poley
Pavel Poley

Reputation: 5597

Firebase storage store url vs reference

For example I have Firestore object with images, how should I store the images?

Should i store array of string(urls) or array of reference to Firebase Storage (not sure if it possible, maybe reference as string)?

It feels like storing a reference is more flexible, for example we can create reference before the image is uploaded and we not depend on the url , but I wonder if it will be correct decision.

Upvotes: 3

Views: 1765

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

By "storage reference" I'll assume you mean the path to the image in your storage bucket. Let's quickly define all three parts in play here:

  1. Each file in a Cloud Storage bucket has a unique path within that bucket.
  2. Each file in Cloud Storage can have a download URL. This URL allows anyone who knows it to read the file through HTTP (regardless of any security rules defined on the file).
  3. To read/write a file through the SDK, you need to create a StorageReference to it, and have permission to read/write the file. From a reference to a file, you can get the download URL and the path to that file.

You can store #1 and/or #2 in a database, but you can't store #3 as references only exist in your application code.

Most commonly you'll see the download URL being shared, as that is the most familiar way to share files with others. And from the download URL, you can always create a reference, and thus also get the path.

You can also just store the path of the file, and then get the download URL as needed. This is slightly less common, as it requires that all users have full read access to the storage bucket, which isn't needed if you share download URLs directly.

I occasionally store both the path and the download URL for the image, to that both are loaded in one go. While either one can easily be mapped to the other, storing both removes the need for that mapping and keeps my code simpler. From how I phrase things I hope it's clear that this is a personal preference.

Upvotes: 8

Babar Bashir
Babar Bashir

Reputation: 203

It depends on your implementation.

  • If you are using Firebase Storage API to get image then store reference.
  • If you want to use Glide/Picasso then store URL

Note : Reference is more secure.

Upvotes: 2

Related Questions