Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: Download Image from Firebase Storage

Pre-Informations:

I still have a Firebase project opened and I am successfully still using the Firebase Database (So the firebase.initializeApp(config) works). I uploaded several Images at my Firebase Storage in the folder "/Images/" and named them image1.jpg, image2.jpg,..

React-Native: 0.31.0, Firebase: 3.3.0

What I want:

I want to get the Image from the Firebase Storage and put them into an <Image>

What I actually did:

var storageRef = firebase.storage().ref('Images/image1.jpg');

//1. Try:
storageRef.getDownloadURL().then(function(url) {
  console.log(url);
)}

//2. Try:
var sampleImage = storageRef.getDownloadURL().then(function(url) {
  console.log(url);
)}

//3. Try:
var sampleImage = storageRef.getDownloadURL();

Informations about the code:

  1. The code doesn't reach the console.log(url) point

    2.1. Tried to use the sampleImage as source in a <Image> object: empty Image

    2.2. Tried to log the sampleImage: undefined

    2.3. Tried to logthe url: didnt reach this point

    3.1. Tried to use the sampleImage as source in a <Image> object: empty Image

    3.2. Tried to log the sampleImage: { F: 0, ka: undefined, o: { F: 0, ... } }

All tries returned the same error code after 2-3 minutes.

Firebase Storage: Max retry time for operation exceeded, please try again.

Thank you very much in advance for you help.

like image 814
Jonathan Stellwag Avatar asked Sep 08 '16 07:09

Jonathan Stellwag


People also ask

How do I download from Firebase Storage react?

Downloading Methods. After creating an appropriate reference, we call various methods to download files, i.e., getBytes(), getStream(), or we can get a download URL with getDownloadUrl() for downloading the file with another library. The use of the getBytes() method is the easiest way of downloading the file to memory.

How can I get image URL from Firebase Storage react?

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

Your first one seems right based on the docs. I believe you're getting an error so wire up an error callback as well:

storageRef.getDownloadURL().then(function(url) {
    console.log(url);
}, function(error){
    console.log(error);
});

See: https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getDownloadURL

like image 194
Mathew Berg Avatar answered Sep 22 '22 17:09

Mathew Berg


Update

I reported this issue to Firebase support and I got the following response:

Our engineers got back and it seems that this is an Android-specific issue (works on iOS) [..] We are currently working on this, but as to the timeline, we could not share anything as of the moment

Research

It appears that this is an actual bug caused by react-native's XMLHttpRequest acting up. When trying the getDownloadURL() in the same way you describe the same thing happens (ie. Max Retry time reached).

However when overriding react-native's polyfill of XMLHttpRequest as described in this stack overflow thread getDownloadURL() will finish normally and return a valid URL.

We are using react-native v0.33.0, Firebase v3.4.1

Workaround

We have been using this workaround in the meantime; bypassing the Firebase API:

var bucket = firebase.storage().ref().bucket;
var firebaseUrl = "https://firebasestorage.googleapis.com/v0/b/" + bucket + "/o/";
var finalUrl = firebaseUrl + 'path%2Fto%2Fresource';
firebase.auth().currentUser.getToken()
.then((token) => {
  fetch(finalUrl, {headers: {'Authorization' : 'Firebase ' + token}})
  .then((response) => response.json())
  .then((responseJson) => {
    var downloadURL = finalUrl + "?alt=media&token=" + responseJson.downloadTokens})
  })
}); 

This is an issue that seems to affect a small amount of users since I have only seen the problem raised otherwise here. However this appears to be an actual bug and it is very debilitating when it occurs, so any more input would be greatly appreciated.

like image 36
Christian Barth Roligheten Avatar answered Sep 19 '22 17:09

Christian Barth Roligheten