Firebase Cloud功能使用url从存储中删除孤立映像

时间:2017-10-30 15:41:28

标签: android firebase google-cloud-storage firebase-storage

在android中我只需调用:FirebaseStorage.getInstance().getReferenceFromUrl(removeMe.getImgUrl()).delete();删除存储在firebase存储中的任何文件

这就是我的index.js现在的样子

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

exports.onDeleteTimelapse = functions.database.ref("/timelapses/{id}")
    .onDelete(event => {

        imagesRef.orderByChild("parentId")
            .equalTo(event.params.id)
            .on("value", function (snapshot) {
                snapshot.forEach(function (childSnapshot) {
                    //image url
                    //file name equals childSnapshot.val().id
                    var imgurl = childSnapshot.val().imgUrl;
                    //prints img url
                    console.log(imgurl + " ");
                    //todo remove <================
                    //remove img from dbs
                    childSnapshot.ref.remove();
                });
            });
    });

使用函数实现此行为的方法是什么。 imgurl 是我的图片存储的网址

1 个答案:

答案 0 :(得分:1)

好的我修好了,随意使用它。你需要导入

const gcs = require('@google-cloud/storage')();

exports.onDeleteTimelapse = functions.database.ref("/timelapses/{id}")
    .onDelete(event => {

        imagesRef.orderByChild("parentId")
            .equalTo(event.params.id)
            .on("value", function (snapshot) {
                snapshot.forEach(function (childSnapshot) {
                    childSnapshot.ref.remove(); //this calls method bellow
                });
            });
    });

exports.onDeleteImage = functions.database.ref("/images/{id}")
    .onDelete(event => {

        const filename = event.params.id;
        gcs
            .bucket(bucketName) // find it in Firebase>Storage>"gs://...." copy without gs 
             //or go to console.cloud.google.com/ buckets and copy name
            .file("images/" + filename) //file location in my storage
            .delete()
            .then(() => {
                console.log(`gs://${bucketName}/${filename} deleted.`);
            })
            .catch(err => {
                console.error('ERROR-DELETE:', err+ " filename: "+filename);
            });

    });