-1

I have a problem regarding firebase storage. I have stored there several .JSON files, my storage looks like this right now:

enter image description here

My goal is to retrieve this .JSON files and use the data inside those files in my react-native app. The thing is, all tutorials online only show how to retrieve pictures from the storage, like this:

const downloadImage = async (imageName) => {
        const imageRef = firebase.storage().ref(`images/${imageName}`);
        const url = await imageRef.getDownloadURL();
        setPickedImage(url);
    };

In this case we retrieve a downloadURL from firebase but in my case we dont have a picture. What I want to accomplish is to be able to look inside the file I download, I want to reuse the data inside those 2 .JSON files and I dont know how to accomplish that and thats where I need your help, thank you for reading!

PS: I know it would be much more easier to just use firebase database for .JSON files but I want explicitly use firebase storage in this case, thats really important to me.

1 Answers1

0

After you have the json file's url you need to use react-native-fs or rn-fetch-blob to download and read the file.

Example, using rn-fetch-blob

import RNFetchBlob from 'rn-fetch-blob';

const downloadUrl = 'xyz'; // url you got from Firebase Storage
const dirs = RNFetchBlob.fs.dirs;
const pathToSavedFile = dirs.DocumentDir + '/path-to-file.json';

// Download the file
RNFetchBlob.config({
  // response data will be saved to this path if it has access right.
  path: pathToSavedFile,
})
  .fetch('GET', downloadUrl, {
    //some headers ..
  })
  .then((res) => {
    // the path should be dirs.DocumentDir + 'path-to-file.anything'
    console.log('The file saved to ', res.path());

    // Read the file
    RNFetchBlob.fs.readFile(pathToSavedFile, 'utf8').then((data) => {
      // data should be JSON string, you should be able to JSON.parse it
      console.log(data);
    });
  });

Simple example, not tested but should work, you can improve it according to your need, like remove nesting and use async await.

On Android you will also have to request permissions to write file:

import {PermissionsAndroid, Platform} from 'react-native';

const requestFileWritePermission = async () => {
  if (Platform.OS === 'ios') return true;
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
  );
  return granted === PermissionsAndroid.RESULTS.GRANTED;
};

Refer to the docs, linked above, for detailed instructions on how to use the library.

Vaibhav Vishal
  • 6,576
  • 7
  • 27
  • 48
  • In this example you made "DocumentDir" is not declared and React Native says its null. How can I resolve this problem? I tried to declare "DocumentDir" as a const one line before but its not beeing noticed my React Native. – Kubaghetto the fresh Testobun Feb 03 '21 at 12:13
  • DocumentDir is in rn-fetch-blob. `RNFetchBlob.fs.dirs.DocumentDir`. If it's null, make sure you have installed the library properly: https://github.com/joltup/rn-fetch-blob#installation. Also on Android you will have to request permission to write file. I will add that in answer. – Vaibhav Vishal Feb 03 '21 at 12:17
  • https://stackoverflow.com/a/59767257/10900354 Im working with expo and just found out that this sollution with RNFetchBlob will not work for me, I will taker a look into your other suggested module – Kubaghetto the fresh Testobun Feb 03 '21 at 12:34
  • I don't think the other module will work either. expo comes with file system access too,: https://docs.expo.io/versions/latest/sdk/filesystem/ Expo's filesystem module has all these features(file download & read) too. expo's sdk is well maintained than these two, so use that as you are already using expo – Vaibhav Vishal Feb 03 '21 at 12:39