了解Firebase存储令牌 [英] Understanding Firebase Storage tokens

查看:120
本文介绍了了解Firebase存储令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解令牌如何在Firebase存储中运行。

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

每当我的网络应用程序将图像上传到FS时,它都会向其公共网址添加一个标记。问题是每当您将相同的图像文件上传到Web应用程序的另一部分时,您似乎没有获得另一个文件,但是已经上传的文件URL的标记不同,因此呈现前一个403错误注册图像显示。

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?

示例:

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

然后将该url显示在img src中的某个位置。

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">

如果用户重复该过程并在应用的另一部分上传相同的picture.jpg,在Firebase存储中获取全新副本时,该文件将被以新标记结尾的URL覆盖;说12345。

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.

所以:

 <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


推荐答案

令牌对于特定版本的上载而言是唯一的。如果用新内容覆盖文件,则会生成一个新的令牌,其中包含一个新的无法使用的URL。

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.

因此换句话说,令牌对于特定的blob是唯一的 - - 它们不是每个存储位置唯一的。我们这样做是为了增加安全措施,以确保开发人员和最终用户不会意外地暴露他们不想要的数据。

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.

但是,您可以翻译存储位置(gs://mybucket/myfile.png)使用我们的js SDK进入下载URL。这样,您可以根据需要传递gs uri,并在想要将其放入图像后将其转换为完整的URL。

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.

请参阅: https://firebase.google.com/docs/reference/js/firebase.storage.Reference.html#getDownloadURL

这篇关于了解Firebase存储令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆