node.js - 如何使用 Firebase Cloud Functions 删除旧文件?
问题描述
我正在为 Firebase Cloud Functions 编写一个函数,我可以在其中从 Firebase 数据库中删除旧节点。每个节点上都有一个指向保存在 Firebase 存储中的文件的链接。
数据库结构
"data" : {
"-Lo0onTCSVeY-Kco5ujX" : {
"photo" : "https://firebasestorage.googleapis.com/v0...",
"idD" : "-Lo0onTCSVeY-Kco5ujX",
"idUs" : "jh4Ch9rBgQPwBTfv43MgNissRUP2",
"timestamp" : 1567693686925,
}
},
功能:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var DAY_MILLIS = 86400000 //1 Day
exports.deleteOldPosts = functions.database.ref('/data/{pushId}').onWrite(function(change) {
var ref = change.after.ref.parent; // reference to the parent
var now = Date.now();
var cutoff = now - DAY_MILLIS;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value').then(function(snapshot) {
// create a map with all children that need to be removed
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});
我该怎么做才能删除文件?提前致谢。
解决方案
这对我有用
const promises = [];
promises.push(ref.update(updates));
const bucket = admin.storage().bucket();
promises.push(bucket.file(`file/${anotherFile}`).delete());
return Promise.all(promises);
推荐阅读
- javascript - 收到的道具在反应本机导航中未定义
- unity3d - Unity 2018.3:如何在整个 3D 模型的一部分上应用纹理?
- python - 解码使用 CP437 表编码的文件
- android - Android,如何合并两个视频文件?
- windows - 在 tcl 中如何获取可用的 RAM 内存量
- generics - Rust 返回泛型特征的已知类型
- mysql - MySQL如何创建一个视图来处理查询参数
- netflix-eureka - server.context-path 在 springboot admin /Eureka 的健康端点中附加了两次
- java - 在 Java 中有没有办法在异常期间知道方法参数的值?
- typescript - TypeScript 映射类型更改值函数的返回类型