0

I'm relatively new to firebase and currently working on node.js (react-electron) app, my goal is to display some picture which was stored in firebase-storage.

In my app, I know the internal firebase storage URL gs://myapp.appspot.com/folders/image.jpg and want to get downloadableURL https://firebasestorage.googleapis.com/...

So here's the problem: I know that there is refFromURL() and getDownloadURL() method from firebase storage and I planned to do this

import firebase from 'firebase';
import config from './firebaseConfig.json';
firebase.initializeApp(config);
var storage = firebase.storage();

function fetchTrueImgsrc(internalSrc) {
  if (internalSrc === undefined) return null;
  return storage.refFromURL(internalSrc).getDownloadURL();
}

And I got this

Line 4: Uncaught TypeError: firebase__WEBPACK_IMPORTED_MODULE_1___default.a.storage is not a function

I have found out that firebase.storage() is no longer support for node.js and all of document on https://firebase.google.com/docs/storage/web/start is totally useless for me

So the question is, how would I implement this in node.js client side if I can't use firebase.storage? please help.

I have tried:

const myappBucket='myapp.appspot.com';
import { Storage } from '@google-cloud/storage';
const bucket = new Storage().bucket(myappBucket);
const file = bucket.file(internalSrc); 
file
  .getSignedUrl({
    action: 'read',
    expires: '03-09-2491'
  })
  .then(signedUrls => {
    console.log('signed URL', signedUrls[0]); // this will contain the picture's url
    return;
  })
  .catch(err => console.error(err));
const myappBucket='myapp.appspot.com';
import config from './firebaseConfig.json';
import firebase_admin from 'firebase-admin';
firebase_admin.initializeApp(config);
var storage = firebase_admin.storage();
var bucket = storage.bucket(myappBucket);
bucket.file(internalSrc).getSignedUrl(
  {
    action: 'read',
    expires: '03-09-2491'
  },
  function(err, url) {
    if (err) {
      console.error(err);
    } else {
      console.log('Download model DL url: ' + url);
    }
  }
);

Both got this error

SigningError: Unexpected error determining execution environment: Failed to fetch at GoogleAuth.

I tried to avoid Downgrading firebase to firebase@v4.7.0 but if there is no solution for this, I think i should try this too.

I'm using: - "@google-cloud/storage": "^2.5.0" - "firebase": "^6.0.2" - "firebase-admin": "^7.3.0" - "react": "^16.8.6" - "electron": "^3.0.10"

thanks.

  • Just to be sure, you want the download url for your file pass in parameter, right ? – DataHearth May 25 '19 at 19:59
  • yes, I pass in `gs://myapp.appspot.com/folders/image.jpg` string for argument and want the function to return `https://firebasestorage.googleapis.com/...` as downloadable URL – Varis Ratanasirisawad May 26 '19 at 05:20
  • > Where are you executing this code? > How are you setting the service account? Are you using the default or another one? > What's the permission for that service account? Does this service account has access to the bucket you are trying to reach? – Noohone May 28 '19 at 10:39

1 Answers1

0

Here's a code example which returns the download url for your file after upload using the ADMIN SDK :

firebase.storage().bucket().upload('your/file', {
                destination: `where/you/want/it`,
            }).catch((error) => {
                throw error;
            }).then((result) => {
                result[0].getSignedUrl({
                    action: 'read'
                }).then((url) => {
                    url[0] // your url
                })
            });

Here's the exact same question : Post

DataHearth
  • 484
  • 6
  • 20
  • thank you @DataHearth but that is isn't what I am intended. I already have this picture file uploaded into storage long time ago by other method. Then I have the `gs://...` string in my app which is not public URL to downloaded it, so what i want is to change it to downloadable URL like `https://firebasestorage.googleapis.com/v0/b/xxxxxxxxxx.appspot.com/o/Folder%2Fxxxxxx.png?alt=media&token=xxxxxxxxxx` or something like this, which I have access to this by going to firebase console. – Varis Ratanasirisawad May 26 '19 at 15:18
  • And btw, I have already tried using Service account from firebase admin SDK and have double check permission in IAM & admin / Google cloud service account platform - which grant permission ascloud functions service agent, service account token creator, editor, etc. [Code Snippets](https://ibb.co/6bX3xHh) Still got this error > Uncaught (in promise) SigningError: Unexpected error determining execution environment: Failed to fetch – Varis Ratanasirisawad May 26 '19 at 17:14