Home > OS >  Cloud Storage for Firebase: How to recover a pdf
Cloud Storage for Firebase: How to recover a pdf

Time:11-18

I have a pdf stored in Cloud Storage and I'm trying to take this file to send it through email.

I'm trying to recover it but I receive back an error about access deniend:

Uncaught (in promise): FirebaseError: Firebase Storage: User does not have permission to access

My code:

 const storageRef = firebase.storage().ref();
 var forestRef = storageRef.child('/uploads/'   offerId   '/'   offerId   '.pdf');

 forestRef.getDownloadURL()
      .then(function (url) {
        console.log("url ", url)
        var xhr = new XMLHttpRequest();
        xhr.responseType = 'blob';
        xhr.onload = function (event) {
          var blob = xhr.response;
        };
        xhr.open('GET', url);
      })

I think that the problem should be that I'm not using the access token but I don't know how to recover it. ( I have tried to use also the getMetadata, but the result is the same)

Edit:

I have also the url with token

CodePudding user response:

The files in firebase storage follow a specific url format. use the following format. The url generated from getDownloadURL() will have token associated with it causing the link to expire after few days.

https://firebasestorage.googleapis.com/v0/b/<PROJECT-NAME>.appspot.com/o/<PATH>/<TOFILE>?alt=media

So your url string for /uploads/${offerId}/${offerId}.pdf will be :

https://firebasestorage.googleapis.com/v0/b/<PROJECT-NAME>.appspot.com/o/uploads/${offerId}/${offerId}.pdf?alt=media

Thus by string manipulations you can create the file urls.

CodePudding user response:

While download URLs provide public, read-only access to files in Cloud Storage for Firebase, calling getDownloadURL to generate such a URL requires that you have read permission on that file.

The error message indicates that the code does not meet your security rules, i.e. that there is no user signed in when you run this code.

If that is not what you expect, I recommend checking that right before you call the Storage API:

const storageRef = firebase.storage().ref();
var forestRef = storageRef.child('/uploads/'   offerId   '/'   offerId   '.pdf');

if (!firebase.auth().currentUser) throw new "No user signed in, can't get download URL";

forestRef.getDownloadURL()
...
  • Related