Firebase 存储:尽管有 public 条规则,但不允许用户访问对象

Firebase Storage: User not permitted to access object despite public rules

我是 Firebase 存储的新手。我制作了一个简单的应用程序,它应该从 Firebase 下载文件。 每次下载都失败并出现以下错误:

StorageException: StorageException has occurred.
User does not have permission to access this object.
Code: -13021 HttpResult: 403

这些是我在 Firebase 上的规则:

service firebase.storage {
  match /b/***.appspot.com/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

我也已经尝试启用匿名身份验证,但是尽管登录成功,这并没有改变任何东西。

编辑:这是我的 java 代码:

FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
mAuth.signInAnonymously().addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {...}


FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
//Create a storage reference from our app
storageRef = firebaseStorage.getReference();
contentDir = new File(context.getFilesDir().getAbsolutePath() + "\content\testfile.txt");
storageRef.getFile(contentDir).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {...}

更新:

不支持从存储根目录下载文件,或者不在默认安全规则范围内。 (也许 match /{allPaths=**} 不包含根目录?)将文件移动到根目录以外的某个位置:

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
storageRef = firebaseStorage.getReference("anyPath");

如果为了测试您想要允许 public 访问您的存储桶,请使用从 Storage Guide 复制的这些规则。 j_h_o_m_o 发布的规则更安全,限制登录用户的访问:

// Anyone can read or write to the bucket, even non-users of your app.
service firebase.storage {
  match /b/***.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

同时检查您的存储桶名称是否与您在 Firebase 控制台中看到的相匹配。例如,如果存储文件选项卡显示 gs://project-12345.appspot.com,您的规则将开始:

match /b/project-12345.appspot.com/o {

注意:这将允许任何人访问您的存储是否经过身份验证 尝试将存储规则更改为此

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth == null;
    }
  }
}