如何限制用户可以上传到 Firebase 存储的文件总大小?
How to limit total size of files that a user can upload to Firebase storage?
我有一个应用程序应该允许用户上传每个用户总计 150 GB 的文件,并且应该阻止用户上传超过 150 GB 的新文件。如何添加此限制?
您必须通过云上传文件 function/server 并跟踪用户上传的总大小:
1. Upload image to your server
2. check the size and add it to total size stored in a database
3. If the user has exceeded 150 GB, return quota exceeded error else upload to Firebase storage
user -> server -> Firebase storage
一个更简单的替代方法是使用 Cloud Storage Triggers which will trigger a Cloud function every time a new file is uploaded. You can check the object size using the metadata and keep adding it in database. In this case, you can store total storage used by a user in custom claims 以字节为单位。
exports.updateTotalUsage = functions.storage.object().onFinalize(async (object) => {
// check total storage currently used
// add size of new object to it
// update custom claim "size" (total storage in bytes)
});
然后您可以编写一个安全规则来检查新对象的大小和所使用的总存储量不超过 150 GB:
allow write: if request.resource.size + request.auth.token.size < 150 * 1024 * 1024 * 1024;
我有一个应用程序应该允许用户上传每个用户总计 150 GB 的文件,并且应该阻止用户上传超过 150 GB 的新文件。如何添加此限制?
您必须通过云上传文件 function/server 并跟踪用户上传的总大小:
1. Upload image to your server
2. check the size and add it to total size stored in a database
3. If the user has exceeded 150 GB, return quota exceeded error else upload to Firebase storage
user -> server -> Firebase storage
一个更简单的替代方法是使用 Cloud Storage Triggers which will trigger a Cloud function every time a new file is uploaded. You can check the object size using the metadata and keep adding it in database. In this case, you can store total storage used by a user in custom claims 以字节为单位。
exports.updateTotalUsage = functions.storage.object().onFinalize(async (object) => {
// check total storage currently used
// add size of new object to it
// update custom claim "size" (total storage in bytes)
});
然后您可以编写一个安全规则来检查新对象的大小和所使用的总存储量不超过 150 GB:
allow write: if request.resource.size + request.auth.token.size < 150 * 1024 * 1024 * 1024;