Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  157] [ 7]  / answers: 1 / hits: 23012  / 8 Years ago, fri, september 2, 2016, 12:00:00

I'm trying to understand how tokens work in Firebase Storage.



Whenever my web app uploads an image to FS it adds a token to its public url. The problem is whenever you upload that same image file to another part of the web app, it seems like you don't get another file, but a different token for the file url that was already uploaded, thus rendering a 403 error for the previous registered image display.



Is there a way to solve this?



Example:



storageRef.put(picture.jpg);
uploadTask.snapshot.downloadURL
// returns something like https://firebasestorage.googleapis.com/v0/b/<your-app>/o/picture.jpg?alt=media&token=09cb2927-4706-4e36-95ae-2515c68b0d6e


That url is then displayed somewhere inside an img src.



<img src=https://firebasestorage.googleapis.com/v0/b/<your-app>/o/picture.jpg?alt=media&token=09cb2927-4706-4e36-95ae-2515c68b0d6e>


If the user repeats the process and uploads the same picture.jpg in another section of the app, instead of getting a brand new copy in Firebase Storage, the file is overwritten with an URL ending with a new token; say 12345.



So:



 <img src=https://...picture.jpg?alt=media&token=12345> // New upload renders fine
<img src=https://...picture.jpg?alt=media&token=09cb2927-4706...> // But old upload breaks because of wrong url

More From » firebase

 Answers
176

Tokens are unique for a particular version of an upload. If you overwrite the file with new content, then a new token will be generated with a new unguessable url.



So in other words, tokens are unique for a particular blob -- they are not unique per storage location. We did this as an increased measure of security to ensure that developers and end users did not accidentally expose data they did not intend.



You can, however, translate the storage location (gs://mybucket/myfile.png) into a download url using our js SDK. That way, you can pass around the gs uri if you wish and translate it to a full URL once you want to place it into an image.



See: https://firebase.google.com/docs/reference/js/firebase.storage.Reference.html#getDownloadURL


[#60834] Wednesday, August 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;