【发布时间】: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
标签: android firebase android-studio firebase-storage firebase-security