0

I have the following code to get the url of images stored in firebase storage.

const firebase = require('firebase');
require('firebase/storage');

firebase.initializeApp(conf);

const firebaseRef = firebase.storage().ref(path);

firebaseRef.getStorage().getDownloadURL().then(url => {
    console.log(url);
}).catch(function (error) {
    console.log(error);
});

When I try to run it (with a correct path) it always ends with the error:

FirebaseStorageError {
  code_: 'storage/canceled',
  message_: 'Firebase Storage: User canceled the upload/download.',
  serverResponse_: null,
  name_: 'FirebaseError'
}

I have this storage rules:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write
      // : if request.auth != null;
    }
  }
}

When I try to upload or delete some file with the nodejs api it works great but at the momment to get the url of one file it always return the same error.

I tryed finding what could cause this but i can't find anything: https://firebase.google.com/docs/storage/web/handle-errors

And of course as a user im not canceling the upload/download stage.

Thank you in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jortega
  • 51
  • 6
  • Can you show how you import `firebase`? Or specifically: what node package you use to access Firebase? The reason for asking is that you tagged with Node.js, but as far as I know the Node.js Admin SDK doesn't have a `getDownloadURL` method, nor can I find the `getStorage()` method you call in the docs for the JavaScript or Node.js SDKs. – Frank van Puffelen Sep 02 '20 at 14:01
  • I edited my question to add te "require(package)" part. Also, i am following this firebase documentation to get this done: https://firebase.google.com/docs/storage/web/download-files – Jortega Sep 03 '20 at 15:15
  • Aha... that's just the regular JavaScript SDK, but using a node bundler. I changed the tags to reflect that, as the API is rather different between Node.js and client-side JavaScript. – Frank van Puffelen Sep 03 '20 at 16:44

1 Answers1

0

I'm pretty sure the problem comes from calling getStorage(), which returns you the root of the storage bucket, on which you can't generate a download URL.

If that is the cause, just remove that call from the chain. So you end up with something like:

const firebaseRef = firebase.storage().ref(path);

firebaseRef.getDownloadURL().then(url => {
    console.log(url);
}).catch(function (error) {
    console.log(error);
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hmm.... I don't see what's goign wrong in that case. Can you set up a reproduction on a site like jsbin, so I can have a look? – Frank van Puffelen Sep 04 '20 at 14:45
  • Okey i will try and post here the link. thanks for your help :) – Jortega Sep 04 '20 at 19:18
  • After testing it in jsbin I realized that this library is made for client side and not for server side. I saw this other answer that explains it: https://stackoverflow.com/a/56597890/7719904 I will mark this answer as the correct one. Thank you again for your help! – Jortega Sep 06 '20 at 18:29