用于在 30 天后删除存储空间的 Firebase 函数



Frank van Puffelen有一个在时间后删除数据库的示例。 https://stackoverflow.com/a/32012520/12253594 https://github.com/firebase/functions-samples/tree/master/delete-old-child-nodes

这会产生奇迹。但我想知道存储是否也可以?

火碱存储结构

结构:体验-> Experience_ID->全高清->图像

我想删除包括Experience_ID。

我在想,像这样:

exports.deleteOldItems = functions.storange.ref('/Experiences/{notification_id}')
.onWrite((change, context) => {
var ref = change.after.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value', 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);
});
});

但我显然没有像数据库部分那样的时间戳。 那我该怎么办?

此致敬意

您有两个主要选项。

将日期编码为文件名称或其元数据,使用 Cloud Storage 列表 API 列出所有可能的文件,并检查文件名或元数据以确定是否应将其删除。 此解决方案不能很好地扩展,因为要列出的可能文件的数量增长得非常大。

镜像有关数据库中每个文件的一些数据。 对于您添加到 Cloud Storage 的每个文件,请在数据库中创建包含文件时间戳和路径的等效记录。由于数据库易于查询,因此只需查询数据库即可查找应删除的文件。

最新更新