Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read value/content of a file from Firebase Storage

Is it possible to read the value a file without using the download function?

Something instead of:

storageRef.child('text.txt').getDownloadURL().then(function() {
  ...
});

Something like:

storageRef.child('text.txt').getValue().then(function(value) {
  alert(value)
});
like image 392
Philipp Avatar asked Feb 02 '17 00:02

Philipp


People also ask

How do I read files from Firebase storage?

To download data from Cloud Storage in your Firebase web app, you get the download URL for the file and then use that (typically in your HTML) to get the data.

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.

How can I get URL of uploaded image in Firebase storage?

Image URL is obtained by uploading an image to firebase bucket and then that can return back a URL that URL is a permanent URL which can be open anywhere. Then a user can use this URL for any purpose in its application.


2 Answers

I had the same trouble with getting content of the file in Firebase Storage, but finally, I found there is no way to read the file content directly. However, I did it like this.

  fileRef.getDownloadURL()
    .then(url => {
      var xhr = new XMLHttpRequest();
      xhr.responseType = 'json'; 
      xhr.onload = function(event) {
        var json= xhr.response;
        console.log(json);      // now you read the file content
      };
      xhr.open('GET', url);
      xhr.send();
    })
    .catch(err => {
        // process exceptions
    })

What is important was configuring the CORS settings. You can find the instructions here.

Skipping this instruction made me consume much time :/
I hope others avoid the same mistakes. Thanks.

like image 71
Bi Wu Avatar answered Sep 28 '22 09:09

Bi Wu


There is currently no available function to directly read a file in Firebase Storage in JavaScript without downloading it first.

You can file a Feature Request here, if you think this would be really useful.

like image 20
AL. Avatar answered Sep 28 '22 09:09

AL.