Can I create download url for my pictures inside firebase storage myself?

Viewed 57

I am using Firebase storage, and in documentation, I see the method getDownloadURL(), but all it does is attaches some token at the end of the URL?

If my pictures are public, can I simply create the download link myself using a template literal and store it in the Firestore without the token?:

so instead of this:

getDownloadURL(userUploadRef).then(async (downloadUrl) => {
const imageUrl = downloadUrl;
// store it to firestore

simply do this:

const imageUrl = `https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/routinePhotos%2F${res.data.docRefId}?alt=media`;
const thumbnailUrl = `https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/routinePhotos%2F${res.data.docRefId}_640x360?alt=media`;
// store links in firestore

Because these links are based on the document Id in the Firestore, even if I change the actual picture in the storage, I wouldn't have to update the link in the Firestore document, since these links will always refer to the same location.

  1. Can I create a link myself?
  2. What is the purpose of the getDownloadUrl()?
  3. What is the need for the extra token URL at the end of the link, If I can access that file without it anyways?
1 Answers
  1. Can I create the link myself?

No, because that token is generated on the server.

  1. What is the purpose of the getDownloadUrl()?

Is to get the download of the uploaded file URL, including the token.

  1. What is the need for the extra token URL at the end of the link, If I can access that file without it anyways?

The token that is added at the end of the URL acts as a security measure to restrict access only to those who know the token. Without it, you cannot access the file. You'll get a permission denied error:

{
    "error": {
        "code": 403,
        "message": "Permission denied."
    }
}

Edit:

If you want to access the files without a token, then you should set the rules like this:

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