有没有办法以编程方式为 Firebase 中的自定义存储桶设置安全规则?

Is there a way to programmatically set up security rules for a custom storage bucket in Firebase?

出于多种业务和技术原因,我们想为每家使用我们解决方案的公司提供一个特定的桶。此存储桶是由公司注册触发的云功能创建的。

要与 Firebase 一起使用,此存储桶必须 "imported" 在 Web 控制台中,以便定义需要调整的默认安全策略。

由于我们无法在长期内手动执行此操作,是否有可能以编程方式在 Admin SDK 中实现此存储桶导入和策略实施?

使用 admin.securityRules() API:

https://firebase.google.com/docs/rules/manage-deploy#use_the_admin_sdk https://firebase.google.com/docs/reference/admin/node/admin.securityRules.SecurityRules

经过多次测试,我很遗憾地说,即使可以使用 Admin SDK 创建新的存储桶,如果不通过 Firebase 控制台手动导入,也无法在客户端使用它。

不能在使用以下代码创建后立即添加安全规则。 Admin SDK 找不到存储桶 (Error: Requested entity was not found.)

[...]
const bucketName = getCompanyBucketName(key)
const storage = new Storage()
const bucket = storage.bucket(bucketName)
const result = await bucket.create({ location: 'EU' })
if (result[0]) {
   const ruleset = `
      service firebase.storage {
         match /b/{bucket}/o {
            match /{allPaths=**} {
            allow read, write: if request.auth != null;
            }
         }
      }
   `
   await admin
      .securityRules()
      .releaseStorageRulesetFromSource(ruleset, bucketName)
   }