【问题标题】:Retrieve prefixes from Google firebase storage files从 Google Firebase 存储文件中检索前缀
【发布时间】:2021-11-20 17:30:17
【问题描述】:

我正在使用以下内容从对 google firebase 存储桶文件列表的引用中检索前缀...但是当我 console.log 时,它们没有记录前缀。

我希望 console.log("PREFIXES" + foldeRef); 显示前缀,但 foldeRef 不返回它出现的任何内容。

const getNewImages = () => {
    
    const listRef = storageRef
      .child("users")
      .child("H7pFwl0uuRV1NMt8y55S42hxpTi2");
    
    // Find all the prefixes and items.
    listRef
      .listAll()
      .then((res) => {
       
        res.prefixes.forEach((foldeRef) => {
          const PREFIX = foldeRef.listAll();
         
          console.log("PREFIXES" + foldeRef);

          // All the prefixes under listRef.
          // You may call listAll() recursively on them.
        });
        res.items.forEach((itemRef) => {
          console.log("Items " + itemRef);

          array.push(itemRef);
          // All the items under listRef.
          //console.log('ARRAY'+array);
          //setTestImage(array);
          //console.log("TEST IMAGE  " + array);

          
        });
      })
      .catch((error) => {
        console.log("ERROR" + error);
        // Uh-oh, an error occurred!
      });
  };

【问题讨论】:

  • itemRef 是否返回任何内容?你有任何错误吗?
  • itemRef 返回指向我的 Firebase 存储中图像的链接,即“gs://.....”,但在这种情况下,我没有使用它,只是将其放在那里以供参考。我也没有收到任何错误。实际上我刚才通过访问图像 URL.name 以不同的方式解决了这个问题,但我仍然很好奇为什么前缀没有被访问。
  • 再次您好,抱歉回复晚了。但是在使用列为what firestore usesAPI 进行测试时,我注意到前缀数组的值实际上是其中包含的子文件夹,而不是完整的前缀/路径。因此,它似乎是为了进一步深入研究,而不是描述对文件的完整引用。我相信name 是你想要的。
  • 是的,这就是我最终弄清楚的。我改为访问每个返回项目的 URL.name...感谢您的帮助!
  • 抱歉,有时我的吸收速度有点慢,需要自己看一下才能意识到你在说什么。您介意发布您在答案中找到的解决方案并接受它吗? (出于知名度的原因)我会自己做,但这感觉就像是在无端夸奖。

标签: javascript firebase react-native google-cloud-platform google-cloud-firestore


【解决方案1】:

对于遇到相同问题时遇到此问题的任何人,itemRef(即从listAll 获得的响应列表中的各个条目)列出了相关项目的整个链接,以获取文件名最好使用文件元数据值name

原因是Google Cloud Storage,这是Firebase's Cloud Storageuses a flat namespace to store objects背后的底层技术,这意味着从服务器的角度来看,没有文件夹或层次结构,只有文件和权限,使用/ 作为分隔符是为了方便用户和排序。

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2018-12-08
    • 2021-10-25
    • 2021-02-01
    • 2018-08-11
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多