Firebase 如何防止滥用文件上传和删除遗留文件?

How to Firebase prevent abusing file upload and remove legacy files?

在 firebase 中,存储功能很舒服。但是我有两个问题要使用存储功能。

  1. 如何防止用户无限制上传文件?例如)如果我可以写 /{uid}/{timestamp}/{fileName} 路径,用户可以上传直到存储爆炸。
  2. 如何追踪遗留文件并删除它们?如果用户上传带有图像文件的聊天消息并删除图像,文件仍然存在于存储中。有什么钩子可以管理这个吗?

谢谢!

好问题:

您可以使用 Firebase Storage Security Rules 对上传实施任意限制。只要文件在名称或元数据中有一段唯一的识别信息,您就可以将其与用户联系起来并强制执行基于时间的访问(以及其他事项)。一个简单的例子:

match /files/{userId}/{fileName} {
  // Only allow the authorized user to write to their files
  allow write: if request.auth.uid == userId;

  // Allow reads if the resource was created less than an hour ago
  allow read: if resource.timeCreated < request.time + duration.value(60, "m");
}

您要求的是垃圾收集系统 :) 简短的回答是,最好是在执行任何文件删除的同时删除客户端上的任何数据库对象,但众所周知,客户端是不可靠的。您还可以设置一个批处理作业(比如每天),从 private backup, checks which blobs are no longer referenced, and deletes them. You can also wait for Google Cloud Functions, and perform actions (such as writing to the database on file upload) to sync this information. Lastly, you can use Object Lifecycle Management 方法中读取数据库数据,以便在一段时间后清理存储桶中的对象(删除、存档等)。