Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 2192
春和景丽
春和景丽 2020-11-22 01:19

After uploading a file in Firebase Storage with Functions for Firebase, I\'d like to get the download url of the file.

I have this :

...

return buck         


        
23条回答
  •  醉梦人生
    2020-11-22 01:51

    Without signedURL() using makePublic()

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    
    admin.initializeApp()
    var bucket = admin.storage().bucket();
    
    // --- [Above] for admin related operations, [Below] for making a public url from a GCS uploaded object
    
    const { Storage } = require('@google-cloud/storage');
    const storage = new Storage();
    
    exports.testDlUrl = functions.storage.object().onFinalize(async (objMetadata) => {
        console.log('bucket, file', objMetadata.bucket + ' ' + objMetadata.name.split('/').pop()); // assuming file is in folder
        return storage.bucket(objMetadata.bucket).file(objMetadata.name).makePublic().then(function (data) {
            return admin.firestore().collection('publicUrl').doc().set({ publicUrl: 'https://storage.googleapis.com/' + objMetadata.bucket + '/' + objMetadata.name }).then(writeResult => {
                return console.log('publicUrl', writeResult);
            });
        });
    });
    

提交回复
热议问题