How to get a list of all files in Cloud Storage in a Firebase app?

后端 未结 19 1514
傲寒
傲寒 2020-11-21 11:25

I\'m working on uploading images, everything works great, but I have 100 pictures and I would like to show all of them in my View, as I get the complete list of

19条回答
  •  佛祖请我去吃肉
    2020-11-21 11:53

    As of May 2019, version 6.1.0 of the Firebase SDK for Cloud Storage now supports listing all objects from a bucket. You simply need to call listAll() in a Reference:

        // Since you mentioned your images are in a folder,
        // we'll create a Reference to that folder:
        var storageRef = firebase.storage().ref("your_folder");
    
    
        // Now we get the references of these images
        storageRef.listAll().then(function(result) {
          result.items.forEach(function(imageRef) {
            // And finally display them
            displayImage(imageRef);
          });
        }).catch(function(error) {
          // Handle any errors
        });
    
        function displayImage(imageRef) {
          imageRef.getDownloadURL().then(function(url) {
            // TODO: Display the image on the UI
          }).catch(function(error) {
            // Handle any errors
          });
        }
    

    Please note that in order to use this function, you must opt-in to version 2 of Security Rules, which can be done by making rules_version = '2'; the first line of your security rules:

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

    I'd recommend checking the docs for further reference.

    Also, according to setup, on Step 5, this script is not allowed for Node.js since require("firebase/app"); won't return firebase.storage() as a function. This is only achieved using import * as firebase from 'firebase/app';.

提交回复
热议问题