简体   繁体   English

从 firebase 存储中获取每个文件的下载 URL

[英]Get the downloadURL for each file from firebase storage

I'm trying to get the downloadURL for each file from the firebase storage.我正在尝试从 firebase 存储中获取每个文件的下载 URL。 Currently I'm displaying the metadata (in this case the name of each file).目前我正在显示元数据(在这种情况下是每个文件的名称)。

  const [fileNames, setFileNames] = useState([]);

  useEffect(() => {
    const getFiles = async () => {
      const storage = await firebase.storage().ref("/recursos");
      const list = await storage.listAll();
      const items = list.items.map((ref) => ref.getMetadata());
      const results = await Promise.all(items);
      setFileNames(results);
    };
    getFiles();
  }, []);

  return (
    <div className="home">
      <SinapseNav />
      <Container className="d-flex align-items-center justify-content-center mt-4 mb-4">
        <Card className="home-card">
          <Card.Body>
            <h3 className="text-center mb-4">Pesquisar Recursos</h3>
            <Form.Control placeholder='Ex: "Trigonometria"' />

            {fileNames.map((file) => {
              return (
                <Container key={file.name}>
                  <ResourceCard title={file.name} />
                </Container>
              );
            })}
          </Card.Body>
        </Card>
      </Container>
    </div>
  );
} 

As explained in the doc , you can call getDownloadUrl() on the items of the ListResult you got through listAll() .文档中所述,您可以对通过listAll()获得的ListResult的项目调用getDownloadUrl() () 。 Exactly the same way than you do with getMetadata() .与使用getMetadata()的方式完全相同。

So the following should do the trick:因此,以下应该可以解决问题:

const getUrls = async () => {
  const storage = firebase.storage().ref("/recursos");
  const list = await storage.listAll();
  const items = list.items.map((ref) => ref.getDownloadUrl());
  const urls = await Promise.all(items);
  // Do whatever you want with the urls Array
};

Note that listAll() has a default pagination size is 1000: It is actually a "helper method for calling list() repeatedly until there are no more results".请注意, listAll()的默认分页大小为 1000:它实际上是“重复调用list()直到没有更多结果的辅助方法”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase存储获取DownloadURL - How to get DownloadURL from Firebase Storage Firebase 存储 - 获取实际数据而不是 downloadURL - Firebase Storage - Get actual data instead of downloadURL 我无法从Firebase Storage(Angular / Ionic)获取图像downloadUrl - I can't get image downloadUrl from Firebase Storage (Angular/Ionic) I need to get the download URL from Firebase, Storage, using JavaScript, in html, The Picture gets uploaded, but I get no DownloadURL? - I need to get the download URL from Firebase, Storage , using JavaScript, in html , The Picture gets uploaded , but I get no DownloadURL? 从客户端访问 Firebase 云存储中的文件时,使用 downloadURL 与 getBlob 获取文件是否更划算? - When accessing files in Firebase Cloud Storage from a client, is it more cost effective to fetch a file using a downloadURL vs getBlob? Firebase 存储不返回 downloadURL - Firebase storage don't return downloadURL 如何将 firebase storage downloadURL 保存到 firestore 集合? - How to save firebase storage downloadURL to firestore collection? 如何在使用 firebase 存储上传后获取调整大小的 downloadUrl(Web SDK + 调整图像扩展) - How to get the resized downloadUrl after upload with firebase storage (Web SDK + Resize images extention) Firebase:从存储中获取 JSON 文件的内容 - Firebase: Get contents of JSON file from storage 如何从此函数获取downloadURL? - How to get downloadURL from this function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM