Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cache the getDownloadUrl() calls on the client with Firebase Firestore?

I store some Firebase Storage paths for images/videos in the backend DB and the mobile client uses these to fetch the download URLs as/when needed for my views:

import {firebase} from '@react-native-firebase/storage';

// e.g. 'images/stars.jpg'
const getDownloadUrlForFilePath = (filePath: string | null): Promise<string> => {
  if (filePath === null) {
    throw 'Path is null!';
  }
  return getDownloadUrlForRef(firebase.storage().ref(filePath));
};

const getDownloadUrlForRef = (ref): Promise<string> => {
  return ref.getDownloadURL();
};

The problem is that I need the same image at many places. Hence I frequently call getDownloadURL() to retrieve the very same download Url. This results in the handset making multiple http requests for the same resource as it appears when I debug the network traffic.

I am not storing the retrieved download URLs in the mobile client anywhere in my current implementation.

I would like to find a way for the app to fetch the download URL only once for the same path and cache it. Is this supported by the Firebase SDK?

like image 580
Crocodile Avatar asked Jan 12 '20 10:01

Crocodile


People also ask

How do you cache data on Firebase?

Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

How can I get download URL from Firebase storage after uploading?

If you already have download infrastructure based around URLs, or just want a URL to share, you can get the download URL for a file by calling the getDownloadUrl() method on a Cloud Storage reference.

How do I stream mp3 audio from Firebase storage?

You have two choices if you want to get files out of a Firebase Storage bucket. Use the full download url that you can get from a Storage Reference that points to the file path in your bucket. Use a download task (Android or iOS) to fetch the data from the file.

How do I enable CORS in Firebase storage?

CORS Configuration To download data directly in the browser, you must configure your Cloud Storage bucket for cross-origin access (CORS). This can be done with the gsutil command line tool, which you can install from here. Run gsutil cors set cors. json gs://<your-cloud-storage-bucket> to deploy these restrictions.


1 Answers

The download URL is valid from the moment it is generated until you revoke it (from the Firebase console). So it is indeed safe to store it, and re-use it.

In fact, most applications that use Cloud Storage through Firebase, generate a download URL right after they upload an image, and then store the download URL in a database, along with metadata about the image.

You'd typically:

  1. Pass the download URL to clients that need unauthenticated access to the image.
  2. Pass the path to clients that need authenticated access to the image, for example as they may need to update the data.

If you only store the download URL (as many apps do), you'll map that download URL back to the corresponding path in the second case by calling the Storage.refFromUrl() method.

like image 98
Frank van Puffelen Avatar answered Nov 02 '22 02:11

Frank van Puffelen