【问题标题】:Firebase Storage: User not permitted to access object despite public rulesFirebase 存储:尽管有公共规则,但用户不允许访问对象
【发布时间】:2017-11-20 22:08:54
【问题描述】:

我是 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>() {...}

【问题讨论】:

    标签: android firebase firebase-security firebase-storage


    【解决方案1】:

    更新:

    从存储根目录下载文件要么不受支持,要么不受默认安全规则的保护。 (也许match /{allPaths=**} 不包括根?)将文件移动到根以外的某个位置:

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

    如果为了测试您想允许公开访问您的存储桶,请使用从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 {
    

    【讨论】:

    • @FlorianBaader:确保存储桶名称正确。当存储桶名称错误时,我得到Code: -13021
    • 感谢您的帮助!我检查了存储桶名称,但没有拼写错误......这真的很令人沮丧(我试图让它从 3 天开始工作)
    • 这需要我一分钟才能找到解释。我记得看过。在存储根目录读取文件有限制。将文件移动到任意路径即可。
    【解决方案2】:

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

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

    【讨论】:

    • 这会检查用户是否经过身份验证,默认情况下需要存储和数据库身份验证。试试看你是否可以使用以下规则访问你的数据库,如果不能,然后粘贴你的错误日志
    • 我只是尝试下载文件。之后我想保护它。但无论如何我尝试了你的解决方案,但它没有工作
    猜你喜欢
    • 2020-05-24
    • 2020-03-19
    • 1970-01-01
    • 2011-04-27
    • 2017-03-10
    • 2020-11-28
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多