【问题标题】:Uploading a file to firebase storage returns 403 error despite changing database rules尽管更改了数据库规则,但将文件上传到 Firebase 存储仍会返回 403 错误
【发布时间】:2020-07-19 13:34:10
【问题描述】:

我正在创建一个使用 HTML 和 JS 将 gif 上传到我的 firebase 存储的基本网页。

这是我的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>Upload gifs</title>
  </head>
  <body>
    <progress value="0" max="100" id="uploader">0%</progress>
    <input type="file" valu="upload" id="fileButton"></input>

  </body>
    <script src="https://www.gstatic.com/firebasejs/7.7.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.7.0/firebase-storage.js"></script>

    <script>
        var firebaseConfig = {
          apiKey: "",
          authDomain: "",
          databaseURL: "",
          projectId: "",
          storageBucket: "",
          messagingSenderId: "",
          appId: "",
          measurementId: ""
        };
    firebase.initializeApp(firebaseConfig);
    console.log(firebase);

    var uploader = document.getElementById('uploader');
    var fileButton = document.getElementById('fileButton');

    fileButton.addEventListener('change', function(e){
      var file = e.target.files[0];

      var storageRef = firebase.storage().ref('gifs/' + "123");

      storageRef.put(file);

      task.on('state_changed', 
        function progress(snapshot) {
            var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
            uploader.value = percentage;
        }
        ,
        function error(err) {

        }

      )
  })

    </script>
</html>

我的 firebase 数据库规则是:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    match /{document=**} {
      allow read, write: if true;
    }
  }
}

尽管当我运行代码时,它返回一个 403 错误,说明它被禁止。我不确定为什么规则已更改为允许访问数据库,但我仍然有问题。将不胜感激。

【问题讨论】:

    标签: javascript firebase google-cloud-storage firebase-security


    【解决方案1】:

    您将 Cloud Firestore(Firebase 提供的数据库服务之一)的安全规则与 Cloud Storage 的规则混淆了。

    您可以在此处找到云存储安全规则的文档:https://firebase.google.com/docs/storage/security

    在您的情况下,从您的问题看来,您希望允许任何人读写,您将编写云存储规则如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2017-11-07
      • 2022-08-17
      • 2021-11-02
      • 2015-05-28
      • 2015-09-24
      • 2020-04-20
      • 2022-01-09
      相关资源
      最近更新 更多