hboylan
hboylan

Reputation: 397

How should I save references to firebase storage objects?

  1. Store references by name (users/1/profile.png). Then the URL needs to be generated all the time.
const url = await firebase
  .storage()
  .ref('users/1/profile.png')
  .getDownloadURL()
  1. Store references by URL. The access token could be revoked which would cause issues trying to generate a new one and update it in the database.
const url = await firebase
  .storage()
  .refFromURL(invalidURL)
  .getDownloadURL()
  1. Related to #1. Store by file name only so files can be moved without having to update database references.
const url = await firebase
  .storage()
  .ref(`users/${user.id}/${user.image}`)
  .getDownloadURL()

Upvotes: 0

Views: 581

Answers (1)

Michael Bleigh
Michael Bleigh

Reputation: 26333

The download URL and the reference path are two different things and I'd store each of them as appropriate (and sometimes both).

Store the Download URL when you want to directly serve the file from storage (e.g. an <img> tag).

Store the Reference Path when you need to keep a reference to the file to modify it later.

Calling getDownloadURL() does trigger a network request and so it's advisable to cache the result when possible to avoid unnecessary extra work/latency.

Upvotes: 2

Related Questions