【问题标题】:Cloud Storage for Firebase SDK 6.1.0 : can't list files in a bucket using listAll()Cloud Storage for Firebase SDK 6.1.0:无法使用 listAll() 列出存储桶中的文件
【发布时间】:2019-10-18 06:03:07
【问题描述】:

我正在尝试使用 listAll() 方法从文件夹中获取所有文件的列表,但无法做到。

这里是代码

$(document).ready(function () {
    var storageRef = firebase.storage().ref(userid + "/");

    console.log(storageRef.listAll());

    storageRef.listAll().then(function (result) {

        result.items.forEach(function (imageRef) {
            imageRef.getDownloadURL().then(function (url) {
                $("#album").append("<div class='col-sm-12 col-lg-4'><div class='card'><div class='card-img-top'><img src='" + url + "'></div></div></div>")
            }).catch(function (error) {

            });
        });
    }).
    catch(function (error) {

    });
});

当前 Firebase 规则:

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

我在控制台中收到此错误Listing objects in a bucket is disallowed for rules_version = "1". Please update storage security rules to rules_verison = "2" to use list.

那么这个错误说明了什么?我需要在 Firebase 控制台中更改规则版本吗?还是我应该制定读写规则以只允许经过身份验证的用户?

【问题讨论】:

    标签: javascript firebase firebase-storage firebase-security


    【解决方案1】:

    在你的安全规则中,你需要声明你要使用安全规则版本2。根据documentation

    自 2019 年 5 月起,Firebase 安全规则第 2 版现已推出 可用的。规则的第 2 版更改了递归的行为 通配符 {name=**}。如果您打算使用,则必须使用版本 2 集合组查询。您必须通过以下方式选择加入版本 2 规则版本='2';安全规则的第一行:

    rules_version = '2';
    

    因此,您的最小规则必须如下所示:

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

    请记住,列表文件是最近在 JavaScript SDK 中发布的,但尚未公布或完整记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 2017-10-03
      • 2013-02-16
      • 2020-06-22
      • 2021-04-05
      • 2019-04-25
      • 2015-02-15
      相关资源
      最近更新 更多