【问题标题】:Firebase (Storage Exception) - User does not have permissionFirebase(存储异常) - 用户没有权限
【发布时间】:2018-04-26 23:48:12
【问题描述】:

目标:允许经过身份验证的用户上传个人资料照片(并将其与该用户关联)。

注意:我关注了用户文档:https://firebase.google.com/docs/storage/security/user-security

问题:我收到错误:

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

StorageException: The server has terminated the upload session
          java.io.IOException: The server has terminated the upload session

测试完成: 我首先有如下规则,并且能够允许经过身份验证的用户上传照片(但它与该用户无关): Authenticated Users

我将我的 规则 更改为以下内容以将图像与用户 ID 相关联: Associate Image to UserID

service firebase.storage {
 match /b/{bucket}/o {
   match /{allPaths=**} {
     allow read: if request.auth != null;
   }
   match /images {
     // Only an individual user can write to "their" images
     match /{userId}/{imageId} {
       allow write: if request.auth.uid == userId;
     }
   }

  }  
}

使用应该将照片与用户 ID 相关联的新规则,我收到关于没有权限的存储异常错误。

如果重要的话,这是我的存储参考:

storage = FirebaseStorage.getInstance();
    storageReference = storage.getReferenceFromUrl("gs://app-name.appspot.com").child("20170702_174811.jpeg"); //was PNG

还有:

public void handleChooseImage(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECTED_PICTURE);  //then goes to onActivityResult
    }

除了文档之外,我还尝试尽可能多地观看有关该主题的 YouTube 视频,但似乎遗漏了一些内容。有人可以指出我正确的方向吗?

【问题讨论】:

    标签: android firebase-realtime-database firebase-authentication firebase-storage firebase-security


    【解决方案1】:

    这里有两个(也许三个)问题:

    1. 您有两条重叠的规则
    2. 您的参考没有指向正确的地方
    3. 我不清楚你是否真的在使用Firebase Auth

    1:您对所有文件都有一个规则 ({allPaths=**}),它只需要身份验证,而第二个更具体的规则需要每个用户身份验证。您可能只想要:

    service firebase.storage {
      match /b/{bucket}/o {
        match /images/{userId}/{imageId} {
          allow write: if request.auth.uid == userId;
        }
      }  
    }
    

    2:您指向的是单个文件名,而不是 /images/{userId}/{imageId} 路径。相反,这样做:

    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    String imageId = "myFile.png"  // or whatever you want to call it
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference ref = storage.getReference()
                             .child("images")
                             .child(userId)
                             .child(imageId);
    

    3:使用 Firebase 身份验证并通过提供商登录(如果您还没有)。

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多