简体   繁体   中英

Cloud function to add/remove the url of a file from Firebase Database that has been added to/removed from Firebase Storage

I'm trying to implement a feature in an app which will list out all the file-names in a ListView and clicking the specific list item will show the contents of the file. I don't really know JavaScript and am also very new to Cloud functions. I wrote a small function which adds metadata to firebase whenever a file is added to firebase storage but it ends up adding the data even when the file is deleted. Here is the code

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.urls = functions.storage.object().onChange( event => {
    const file = event.data;
    const metadata = file.metadata;
    console.log(metadata);
return admin.database().ref("/").set(metadata); });

Can someone please tell me what changes I could make in the above to add urls instead of metadata and also remove the same if the file is being removed?

From the Firebase documentation section Detect resourceState and other event attributes :

The resourceState attribute for an event has the value "exists" (for object creation and updates) or "not_exists" (for object deletion and moves).

 // Exit if this is a move or deletion event. if (resourceState === 'not_exists') { console.log('This is a deletion event.'); return; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM