Home > other >  How to Download video file from firebase storage link as mp4
How to Download video file from firebase storage link as mp4

Time:08-08

Q: Say I have this link to a Video from firebase storage in a collection document. How could I download the Video in mp4 format from this link?

Any suggestions?

const storageLink = https://firebasestorage.googleapis.com/v12/p/project/r/folder%ajfq983fhaogh48h98hg0?alt=media&token=aoi2m3jf80j3f-a02m38jf9qh3f-nfoug43hg9s-auriubfn

Desired solution:

const mp4File = someTransformFunctionToMP4(storageLink)

I have a feeling it might involve https://firebase.google.com/docs/reference/js/v8/firebase.storage.Storage#reffromurl

CodePudding user response:

See: https://firebase.google.com/docs/storage/web/download-files

Once you have the reference, you can get a download URL using the getDownloadURL() function. You can then fetch that like any other URL.

Instead of a download URL, you can also use the reference with the getBlob() (for web browsers), getStream() (for Node), or getBytes() methods, as you prefer.

Here's an example of downloading with a download URL:

import { getStorage, ref, getDownloadURL } from "firebase/storage";

const storage = getStorage();
getDownloadURL(ref(storage, 'images/stars.jpg'))
  .then((url) => {
    // `url` is the download URL for 'images/stars.jpg'

    // This can be downloaded directly:
    const xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = (event) => {
      const blob = xhr.response;
    };
    xhr.open('GET', url);
    xhr.send();

    // Or inserted into an <img> element
    const img = document.getElementById('myimg');
    img.setAttribute('src', url);
  })
  .catch((error) => {
    // Handle any errors
  });

Note that if you're downloading into a web browser, you'll want to enable CORS for the bucket. See https://firebase.google.com/docs/storage/web/download-files#cors_configuration

  • Related