繁体   English   中英

Firebase 云存储:权限被拒绝。 检索 downloadURL 时无法执行此操作 firebase

[英]Firebase Cloud Storage: permission denied. could not perform this operation firebase when retrieving downloadURL

我有一个成功将图像上传到我的云存储桶的 function。

使用另一个 function 我想获得图像的 URL 以在我的页面上显示它(使用<img.../>

getImageUrl(id: string) {
    return this.storageRef.child('X/' + id ).getDownloadURL();

但是当我这样做时..我得到一个'无效' URL 并且当我复制 URL 和 go 给它时,我收到以下消息:

{
  "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
  }
}

我在某处读到这可能是因为 URL 中没有附加令牌,但我该如何启用它?

在过去的几天里,我一直在尝试理解 Firebase 存储规则,但我不知道为什么,但是当我将编写和阅读的规则分开时,例如:

allow write: if request.auth != null && request.resource.size < 3 * 1024 * 1024;
allow read: if true;

代码运行良好,我可以使用 getDownloadURL() 进行读写,但是当我像这样一起使用它们时:

allow read, write: if request.auth != null && request.resource.size < 3 * 1024 * 1024;

我得到了和你一样的错误:

{
  "error": {
  "code": 403,
  "message": "Permission denied. Could not perform this operation"
  }

}

一起使用它们时我可以写,但是当我尝试使用 getDownloadURL() 读取文件时,问题就出现了。 也许您可以尝试像我提到的那样分离规则,看看它是否有效。 我希望它能解决你的问题。 另外,不要忘记规则在设置后 5 分钟后生效 祝你好运。

您必须设置存储安全规则, 在此处阅读有关此内容的更多信息

我的项目刚刚遇到类似的问题,如果有人仍在为此苦苦挣扎,您必须从 Firebase 控制台为您的云存储提供适当的规则。 查看链接以获取有关 rools 的完整详细信息。

如果您在存储中上传任何对象,则需要在 Firebase 控制台 > 存储 > 规则下添加write规则。

我在访问我的 iOS 项目中的存储文件时遇到了同样的问题。 我没有任何自定义安全规则。 只是配置中的默认值。 打破规则换行有帮助!

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

我不知道这是否是一些 firebase 错误,但它有所帮助:)

只需将此规则用于图像

service firebase.storage {
 match /b/{bucket}/o {
 match /images/{imageId} {
  // Only allow uploads of any image file that's less than 5MB
  allow write: if request.resource.size < 5 * 1024 * 1024
               && request.resource.contentType.matches('image/.*');
    }
   }
  }

您需要使用 getDownloadURL 方法。 这将允许您获取下载图像或在您自己的 html 中引用所需的 URL。

请参阅下面的参考文档:

https://firebase.google.com/docs/storage/web/download-files#download_data_via_url

我能够通过将 FirebaseStorage 安全规则从默认更改为:

    rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

在不同的行中分离读取和写入可以解决问题,此外,在您的 url 中,您只需附加 ?alt=media 即可在屏幕上呈现它。

允许写入:如果为真; //你的条件

允许读取:如果为真;

Firebase 规则

 rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if true; } } }

就我而言,我之前不小心删除了文件,因此它需要显示File not found消息,但不知道为什么它显示Permission denied消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM