云存储拒绝权限 Android Studio
Posted
技术标签:
【中文标题】云存储拒绝权限 Android Studio【英文标题】:Cloud storage Denied Permission Android Studio 【发布时间】:2021-07-05 06:42:57 【问题描述】:所以我在使用模拟时向我的 firebase 存储添加了一些安全规则,它按预期工作,但是当我从我的应用程序实际执行时,它显示了这个错误
2021-04-09 16:43:00.322 17375-17375/com.margsapp.messenger E/StorageException: StorageException has occurred.
User does not have permission to access this object.
Code: -13021 HttpResult: 403
2021-04-09 16:43:00.323 17375-17375/com.margsapp.messenger E/StorageException: The server has terminated the upload session
java.io.IOException: The server has terminated the upload session
Caused by: java.io.IOException: "error": "code": 403, "message": "Permission denied. Could not perform this operation"
所以我简化了日志猫
这是我的安全规则
rules_version = '2';
service firebase.storage
match /b/bucket/o
match /allPaths=**
allow read: if request.auth != null;
match/ProfileImages/userId //Am trying to upload an image to this directory.
allow read: if request.auth != null;
allow write: if request.auth != null && request.auth.uid == userId;
match/GroupImages
allow read,write: if request.auth != null;
如果需要上传图片的代码我会上传 如果我在规则上犯了任何错误,请告诉我正确的做法。
编辑:这是上传图片的代码
storageReference = FirebaseStorage.getInstance().getReference("/ProfileImages/"+firebaseUser.getUid());
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICK && resultCode == RESULT_OK && data != null)
imageUri = data.getData();
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK)
loadingBar.setTitle("Profile Image");
loadingBar.setMessage("Please wait, while we update your profile picture...");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(false);
assert result != null;
Uri resultUri = result.getUri();
StorageReference filepath = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(resultUri));
uploadTask = filepath.putFile(resultUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>()
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
if (!task.isSuccessful())
throw task.getException();
return filepath.getDownloadUrl();
).addOnCompleteListener(new OnCompleteListener<Uri>()
@Override
public void onComplete(@NonNull Task<Uri> task)
if (task.isSuccessful())
Toast.makeText(edit_profile.this, "Image has been stored in our servers", Toast.LENGTH_SHORT).show();
Uri downloadUri = task.getResult();
assert downloadUri != null;
String mUri = downloadUri.toString();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL", mUri);
reference.updateChildren(map);
loadingBar.dismiss();
);
else
Toast.makeText(this, "Profile Picture updation is cancelled", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
private String getFileExtension(Uri uri)
ContentResolver contentResolver = edit_profile.this.getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
【问题讨论】:
请向我们展示产生该错误的代码。 我已经添加了上传到图像的代码,我也已经添加了 LogCat 【参考方案1】:要解决您的问题,您需要匹配以下所有路径:
ProfileImages -> userId
在您的存储分区中。所以你的规则应该是这样的:
rules_version = '2';
service firebase.storage
match /b/bucket/o
match /allPaths=**
allow read: if request.auth != null;
match /ProfileImages/userId
match /allPaths=**
allow read: if request.auth != null;
allow write: if request.auth != null && request.auth.uid == userId;
【讨论】:
我已经应用了规则,但是当我运行模拟时它拒绝访问,甚至使用用户 ID 进行身份验证 这只是ProfileImages -> userId
的一个例子。请再检查一次。我刚刚更新了我的答案。
好的,非常感谢它的工作,但我不得不对你的代码做一个小的改动,你实际上错过了提到规则版本,所以当我添加它时,它再次按预期工作非常感谢你帮助
很高兴听到它有效。刚刚用“rules_version = '2';”更新了我的答案。以上是关于云存储拒绝权限 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章
云对象存储错误 - 创建存储桶需要 Writer 或更高权限
Android Studio和Firebase中的权限被拒绝错误[重复]