0

Building with React native I have used Firebase storage to upload files and return a download url to store in the database and save space but, If I want to allow a user to share images/videos to instagram via expo-share or react-native-share, It seems I need to have a local file URL. Is there a way to download the file from storage and save locally then pass? Has anyone else figured out how to share from the cloud?

was attempting:

 //imageUrl = getDownloadURL() stored from firebase storage




const shareToInstagram = async (image: {type: string, url: string}) => {
    
    const storage = fbApp.storage()

    const fileRef = storage.refFromURL(image.url)
 
    fileRef.child(image.url).getDownloadURL()
    .then((url) => {
      const imagePath =  `${Platform.OS==="android"?"/storage/emulated/0/Download":fs.TemporaryDirectoryPath}/${((Math.random() * 1000) | 0)}.jpeg`;
      fs.downloadFile({
        fromUrl: url,
        toFile: imagePath
    }).promise
        .then((result: any) => {
          console.log(imagePath, result);  //here you get temporary path
           Sharing.shareAsync(imagePath);
    })
      .catch((e: any) => {
        console.log(e,"error");
      })
    })

       

  };

FirebaseError: Firebase Storage: Object 'tricks/8eb4ff46-4b96-44cb-b52c-390550d96b49/https:/firebasestorage.googleapis.com/v0/b/aerial-blackbook.appspot.com/o/tricks%2F8eb4ff46-4b96-44cb-b52c-390550d96b49?alt=media&token=0a3cc5cc-ff99-4788-9b9e-017450cfe809' does not exist. (storage/object-not-found)

ryannnnnn
  • 19
  • 6
  • Are you asking [how to download a file from a URL in React Native/Expo](https://www.google.com/search?q=how+to+download+a+file+from+a+URL+in+React+Native%2FExpo)? – Frank van Puffelen Jul 12 '23 at 03:25
  • I think so yes, I think i need the actual file to share so I need to get the stored file back from firebase. – ryannnnnn Jul 12 '23 at 14:17
  • That'd be: https://firebase.google.com/docs/storage/web/download-files – Frank van Puffelen Jul 12 '23 at 14:54
  • Okk so maybe I get the download url and then follow the first link you sent and download the file to some local storage then I can share/Edit!? – ryannnnnn Jul 12 '23 at 15:37
  • I edited the code. the ref is currently returning the error does not exist? does this have to do with the url characters being escaped? I dont even really understand what that means. – ryannnnnn Jul 12 '23 at 17:21
  • Im also confused because the image.url I am passing in is the getDownloadURL() that is stored in the firestore from storage... so do I even need to do this? – ryannnnnn Jul 12 '23 at 17:29
  • If `//imageUrl = getDownloadURL() stored from firebase storage` is true, then this sequence is a wasteful noop: `const fileRef = storage.refFromURL(image.url); fileRef.child(image.url).getDownloadURL()...`. You can just fetch the data from `imageUrl`.. But if you look at the error message, you'll see that the string/URL in it, is not actually a regular URL, but it has some unexpected `tricks/8eb4ff46-4b96-44cb-b52c-390550d96b49/` prefix. – Frank van Puffelen Jul 12 '23 at 18:52
  • Thanks I got it!! The imageURL was correct, it just had to be stored with expo file system and then passed to expo share. – ryannnnnn Jul 12 '23 at 19:15

1 Answers1

1

for anyone else struggling to understand this : This is what worked to share from firestore to instagram via react native with expo:

const shareToInstagram = async (image: {type: string, url: string}) => {

        const callback = (downloadProgress: any) => {
            const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
            console.log(progress);
        }
            const fileType = image.type === 'image' ? 'jpeg' : '.mp4'
    
            const downloadResumable = FileSystem.createDownloadResumable(
                image.url,
                FileSystem.documentDirectory + fileType,
                {},
                callback
            );
        
            try {
                const { uri } = await downloadResumable.downloadAsync();
                console.log('Finished downloading to ', uri);
                Sharing.shareAsync(uri);
            } catch (e) {
                console.error(e);
            }
      };
ryannnnnn
  • 19
  • 6