【问题标题】:Firebase Cloud functions remove orphan image from storage using urlFirebase Cloud 函数使用 url 从存储中删除孤立图像
【发布时间】:2018-04-11 15:56:15
【问题描述】:

在 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 是存储我的图片的网址

【问题讨论】:

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


    【解决方案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);
                });
    
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 2020-10-22
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      相关资源
      最近更新 更多