首页 > 解决方案 > react-native:firestore - 如何从存储桶引用中获取文件夹名称?

问题描述

如果我需要从文件夹中获取图像 url,那么我将执行以下操作:

const ref = storage().ref('foldername');
ref
    .listAll()
    .then(function (result) {
      result.items.forEach(function (imageRef) {
        // And finally display them
        displayImage(imageRef);
      });
    })
    .catch(function (error) {
      console.log(error);
      // Handle any errors
    });

  function displayImage(imageRef) {
    imageRef
      .getDownloadURL()
      .then(function (url) {
        // TODO: Display the image on the UI

        console.log('urlurlurlurl', url);
      })
      .catch(function (error) {
        // Handle any errors
      });
  }

但我需要先获取文件夹名称列表才能在应用程序中显示它们的名称。

例如 - 我的存储桶包含 3 个文件夹。每个文件夹包含 5 张图像。

我需要先获取这 3 个文件夹的列表。

我试图在这里找到答案,但找不到。

谢谢

更新: 这是我的带有 2 个目录的存储桶。这两个目录有图像文件 在此处输入图像描述

我需要先获取这些目录名称,以便在屏幕上显示它们。然后我将根据客户端的目录选择来获取文件。

标签: firebasereact-nativegoogle-cloud-platformfirebase-storagereact-native-firebase

解决方案


现在,您的代码仅通过迭代查看具有给定前缀的文件result.items

ref
    .listAll()
    .then(function (result) {
      result.items.forEach(function (imageRef) {
        // And finally display them
        displayImage(imageRef);
      });
    })

如果您想知道嵌套前缀的名称(您称之为“文件夹”),您也必须进行迭代result.prefixes。React Native 文档似乎没有提供这方面的示例,但您可以在Firebase 文档中看到它。

ref
    .listAll()
    .then(function (result) {
      result.items.forEach(function (imageRef) {
        // ...
      });
      result.prefixes.forEach(function (folderRef) {
        // ...
      });
    })

推荐阅读