Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i implement a CDN with Firebase Storage?

I'm currently working on a social media app for Android mobile, and I have a problem regarding the cost-efficiency of my app especially with Firebase Storage. Therefore I want to implement a CDN for Firebase, which would cache the videos and images, preferably using a CDN service that offers scalable pricing (Google CDN offers this price model). I have been searching everywhere on how I could implement the google CDN with Firebase Storage but found no clear instructions. How can I achieve this?

like image 816
Ale4303 Avatar asked Apr 24 '20 13:04

Ale4303


2 Answers

There are a few ways to implement a CDN with Firebase Cloud Storage.

Please have a look at the documentation Firebase Cloud Storage:

Cloud Storage stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs, and do server-side processing such as image filtering or video transcoding using Google Cloud Platform.

As a result, you can follow the documentation Setting up Cloud CDN with a backend bucket, but keep in mind that Cloud CDN will only kick in if you're accessing your data via that External HTTP(S) Load Balancer. You can find an example in the updated article Create a Custom Domain CDN with Google mentioned by @Yanan C.

As an alternative, you can use Cloudflare or other CDN. To do it you can follow instructions provided in the article How to Put a CDN in Front of Firebase Cloud Storage:

  1. In Cloudflare create a new CNAME pointing your subdomain to c.storage.googleapis.com. For example: CNAME images.firerun.io c.storage.googleapis.com.
  2. In the Firebase console -> Storage, create a new bucket named the subdomain. In the example above, the bucket name would be "images.firerun.io". This is restriction where only the bucket named the same as the subdomain will work.
  3. Add a test file, for example: keep-calm.jpg
  4. Next, give public access to the bucket the Google Console. Note, this is different from Firebase security rules where you should set the security rules to allow externally allow read:
  5. Go to Storage Management in the Google Console. Select your Firebase project.
  6. Click on your new bucket (e.g. images.firerun.io) and click on the tab "permissions."
  7. Click the "Add Members" button.
  8. Enter New Member as "allUsers" and Role as Cloud Storage -> Storage Object Viewer".
  9. Click "Save" and accept the warning that this is publicly accessible.

Now go to your subdomain with the file appended. For example: https://images.firerun.io/keep-calm.jpg

In addition, please have a look on the article Why You Should Put a CDN Like Cloudflare in Front of Firebase.

like image 63
Serhii Rohoza Avatar answered Sep 22 '22 08:09

Serhii Rohoza


For Firebase Storage, I suppose the contents to cache, such as videos and images, are objects stored in buckets, please let me know otherwise. If it is the case, the post provides an answer from Firebase Support regarding how to use CDN with Firebase Storage, with an excerpt as below:

Now, there are ways to add a CDN to Cloud Storage content; I found this small guide using Google CDN: https://medium.com/@marco_37432/create-a-custom-domain-cdn-with-google-beta-7ad9531dfbae

Another use that I have seen is creating a static website with Cloud Storage and adding on top another CDN provider, like CloudFlare. You can see more details in this links: https://cloudplatform.googleblog.com/2015/09/push-google-cloud-origin-content-out-to-users.html https://cloud.google.com/interconnect/docs/how-to/cdn-interconnect

Hope it helps.

like image 20
Yanan C Avatar answered Sep 25 '22 08:09

Yanan C