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

后端 未结 19 1071
傲寒
傲寒 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:54

    Extending Rosário Pereira Fernandes' answer, for a JavaScript solution:

    1. Install firebase on your machine
    npm install -g firebase-tools
    
    
    1. On firebase init set JavaScript as default language
    2. On the root folder of created project execute npm installs
       npm install --save firebase
       npm install @google-cloud/storage
       npm install @google-cloud/firestore
       ... 
    
    1. Add non-default dependencies on your project like
        "firebase": "^6.3.3",
        "@google-cloud/storage": "^3.0.3"
    

    functions/package.json

    {
      "name": "functions",
      "description": "Cloud Functions for Firebase",
      "scripts": {
        "lint": "eslint .",
        "serve": "firebase serve --only functions",
        "shell": "firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log"
      },
      "engines": {
        "node": "10"
      },
      "dependencies": {
        "@google-cloud/storage": "^3.0.3",
        "firebase": "^6.3.3",
        "firebase-admin": "^8.0.0",
        "firebase-functions": "^3.1.0"
      },
      "devDependencies": {
        "eslint": "^5.12.0",
        "eslint-plugin-promise": "^4.0.1",
        "firebase-functions-test": "^0.1.6"
      },
      "private": true
    }
    
    
    1. Create sort of a listAll function

    index.js

    var serviceAccount = require("./key.json");
    const functions = require('firebase-functions');
    
    const images = require('./images.js');
    
    var admin = require("firebase-admin");
    
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://.firebaseio.com"
    });
    
    const bucket = admin.storage().bucket('.appspot.com')
    
    exports.getImages = functions.https.onRequest((request, response) => {
        images.getImages(bucket)
            .then(urls => response.status(200).send({ data: { urls } }))
            .catch(err => console.error(err));
    })
    
    

    images.js

    module.exports = {
        getImages
    }
    
    const query = {
        directory: 'images'
    };
    
    function getImages(bucket) {
        return bucket.getFiles(query)
            .then(response => getUrls(response))
            .catch(err => console.error(err));
    }
    
    function getUrls(response) {
        const promises = []
        response.forEach( files => {
            files.forEach (file => {
                promises.push(getSignedUrl(file));
            });
        });
        return Promise.all(promises).then(result => getParsedUrls(result));
    }
    
    function getSignedUrl(file) {
        return file.getSignedUrl({
            action: 'read',
            expires: '09-01-2019'
        })
    }
    
    function getParsedUrls(result) {
        return JSON.stringify(result.map(mediaLink => createMedia(mediaLink)));
    }
    
    function createMedia(mediaLink) {
        const reference = {};
        reference.mediaLink = mediaLink[0];
        return reference;
    }
    
    
    1. Execute firebase deploy to upload your cloud function
    2. Call your custom function from your app

    build.gradle

    dependencies {
    ...
      implementation 'com.google.firebase:firebase-functions:18.1.0'
    ...
    }
    

    kotlin class

      private val functions = FirebaseFunctions.getInstance()
      val cloudFunction = functions.getHttpsCallable("getImages")
      cloudFunction.call().addOnSuccessListener {...}
    

    Regarding the further development of this feature, I ran into some problems that might found here.

提交回复
热议问题