我一直在寻找一些问题,但无法弄清楚我做错了什么。
我正在尝试将文件上传到 Firebase 存储,然后将下载网址写入数据库的节点内。
现在,这是奇怪的事情,我正在使用电子邮件和密码提供程序进行身份验证,但奇怪的是代码将我的图像上传到存储,但不断循环以将下载链接放入我的数据库中,然后给出我这个错误:
E/StorageException: StorageException has occurred. User does not have permission to access this object.
现在,我已经检查了我的规则,并且由于我已通过身份验证,所以我尝试了这两个规则,但没有成功
service firebase.storage {
match /b/my-bucket.appspot.com/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
还有这个
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
现在我也尝试了这个
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
仍然遇到同样的问题。
这是我用来将文件上传到存储并将下载网址放入数据库的代码
public void cargarProductoFirebase(final String nombreProducto, final float precioProducto, final Dialog dialog, final ProgressDialog progressDialog, Uri filePath) {
mStorageReference.child("fotos").child(mAuth.getCurrentUser().getUid()).child(filePath.getLastPathSegment()).putFile(filePath).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 mStorageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Map<String, Object> producto = new HashMap<>();
producto.put("nombreProducto", nombreProducto);
producto.put("precioProducto", precioProducto);
producto.put("imagen",downloadUri.toString());
mDatabase.child("Usuarios").child(mAuth.getCurrentUser().getUid()).child("productos").push().updateChildren(producto).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
dialog.dismiss();
progressDialog.dismiss();
Toast.makeText(mContext, "Se cargo el producto correctamente.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(mContext, "Error al cargar el producto" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
错误的堆栈跟踪
2018-10-09 20:32:49.442 9767-9821/com.example.macbook.firebasemvp E/StorageException: StorageException has occurred. User does not have permission to access this object. Code: -13021 HttpResult: 403 2018-10-09 20:32:49.443 9767-9821/com.example.macbook.firebasemvp E/StorageException: { "error": { "code": 403, "message": "Developer credentials required." }} java.io.IOException: { "error": { "code": 403, "message": "Developer credentials required." }} at com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage@@16.0.2:455) at com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage@@16.0.2:3435) at com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage@@16.0.2:65) at com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage@@16.0.2:57) at com.google.firebase.storage.zzc.run(com.google.firebase:firebase-storage@@16.0.2:68) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)
文件也上传到正确的位置,但错误仍然存在,无法将该下载网址放入数据库
编辑
我缩小了代码并删除了数据库部分,但仍然是同样的问题
public void cargarProductoFirebase(final String nombreProducto, final float precioProducto, final Dialog dialog, final ProgressDialog progressDialog, Uri filePath) {
mStorageReference.child("fotos").child(mAuth.getCurrentUser().getUid()).child(filePath.getLastPathSegment()).putFile(filePath).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 mStorageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.e(TAG, "onComplete: Success " );
} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
图片:
此外,没有像旧存储实现那样的 addOnProgressUpdate
?
最佳答案
问题是由您调用 getDownloadUrl()
引起的:
return mStorageReference.getDownloadUrl();
我的猜测是,mStorageReference
指向您的 Cloud Storage 存储桶的根目录,因此您需要提供整个存储桶的下载网址,这是不允许的。
要解决这个问题,至于您实际写入的 StorageReference
的下载 URL:
StorageReference fileReference = mStorageReference.child("fotos").child(mAuth.getCurrentUser().getUid()).child(filePath.getLastPathSegment())
fileReference.putFile(filePath).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 fileReference.getDownloadUrl();
}
...
顺便说一句:我通过从错误消息](https://www.google.com/search?q=firebase+storage+“开发人员+凭据+必需”)中搜索“所需的开发人员凭据”发现了这一点。
关于java - Firebase 存储错误尝试将数据插入我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730647/