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?
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With