我可以自己在Firebase存储中为图片创建下载URL吗?

发布于 2025-02-08 10:01:43 字数 816 浏览 2 评论 0原文

我正在使用firebase存储,在文档中,我看到了方法getDownloadurl(),但是它所做的只是附加在URL末尾的令牌吗?

地使用模板文字自己创建下载链接并在没有令牌的

如果我的图片是公开的

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

,我可以简单

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

情况下 将其存储在Firestore中吗? ,即使我更改存储中的实际图片,我也不必更新Firestore文档中的链接,因为这些链接始终指的是同一位置。

  1. 我可以自己创建一个链接吗?
  2. getDownloadurl()的目的是什么?
  3. 如果我可以在没有该文件的情况下访问该文件,那么

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-02-15 10:01:44
  1. 我可以自己创建链接吗?

不,因为该令牌是在服务器上生成的。

  1. getDownloadurl()的目的是什么?

是要下载上传的文件URL,包括令牌。

  1. 如果我可以在没有该文件的情况下访问该文件时,需要在链接结束时额外的令牌URL吗?

在URL末尾添加的令牌是一种安全措施,仅将访问权限限制在知道令牌的人中。没有它,您将无法访问文件。您将获得拒绝错误的权限:

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

编辑:

如果要访问没有令牌的文件,则应设置这样的规则:

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read; //

  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;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文