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
2
Answers
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.So your url string for
/uploads/${offerId}/${offerId}.pdf
will be :Thus by string manipulations you can create the file urls.
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 haveread
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: