我在Firebase存储中设置了一个规则,以限制用户上传不超过20MB的文件,
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow write: if request.auth !=null;
allow read: if request.auth !=null
&& request.resource.size < 20 * 1024 * 1024;
}
}
}
这很好,但问题是当视频由于超限而被服务器拒绝时,没有收听者收到该消息或错误。
我尝试了以下方法,但没有成功
UploadTask uploadTask = FirebaseStorage.getInstance().getReference()
.child("VideoText")
.child(VideoIdentifire)
.putFile(SendVideo);
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
long progress = 0;
progress = (long) (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
Toast.makeText(getContext(), "Uploading is " + progress + "% done", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Fail" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
Toast.makeText(getContext(), "cancel", Toast.LENGTH_SHORT).show();
}
})
.addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(), "paused", Toast.LENGTH_SHORT).show();
}
})
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
});
我该如何解决这个问题?
您的规则拒绝对大于20MB的资源进行read操作,而您的代码是write文件。要拒绝write操作,您需要将大小检查添加到写入规则中(也是):
allow write: if request.auth !=null;
&& request.resource.size < 20 * 1024 * 1024;