react-native:firestore-如何从存储桶引用中获取文件夹名称?

时间:2020-08-05 05:11:04

标签: firebase react-native google-cloud-platform firebase-storage react-native-firebase

如果我需要从文件夹中获取图片网址,请执行以下操作:

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个文件夹的列表。

我正在尝试找到答案here,但找不到答案。

谢谢

更新: 这是我的带有2个目录的存储桶。这2个目录具有图像文件 enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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