尝试将数据插入我的数据库的Firebase存储错误 [英] Firebase Storage bug trying to insert data into my database

查看:54
本文介绍了尝试将数据插入我的数据库的Firebase存储错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一些问题,但是无法弄清我做错了什么.

I have been looking around some questions but can't get to the point of what I'm doing wrong.

我正在尝试将文件上传到Firebase存储,然后将下载URL写入数据库中的节点内.

I'm trying to upload a file to Firebase storage and then write the download URL inside a node in my database.

现在,这很奇怪,我正在使用电子邮件和密码提供程序进行身份验证,但是很奇怪的是,代码将我的图像上传到存储设备,但是不断循环以将下载链接放置在数据库中,然后仅给出我这个错误:

Now, this is the weird thing, I'm authenticating with email and password provider, but the weird thing is that the code uploads my image to the storage but keeps looping to place the download link in my database and then just gives me this error:

E/StorageException:发生StorageException. 用户无权访问此对象.

E/StorageException: StorageException has occurred. User does not have permission to access this object.

现在,我已经检查了规则,并且由于通过了身份验证,因此我尝试了这两个规则,但均未成功

Now, I have checked my rules and since I'm authenticated I tried with this two without any success

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;
    }
  }
}

仍然存在相同的问题.

这是我用来将文件上传到存储并将downloadurl放入我的数据库中的代码

This is the code I use to upload a file to the storage and place the downloadurl in my database

 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. 用户没有访问此对象的权限. 代码:-13021 HttpResult:403 2018-10-09 20:32:49.443 9767-9821/com.example.macbook.firebasemvp E/StorageException:{ 错误":{代码":403,消息":开发人员凭据 必需的." }} java.io.IOException:{错误":{代码":403,消息":需要开发者凭证." }} com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage @@ 16.0.2:455) com.google.firebase.storage.obfuscated.zzj.zza(com.google.firebase:firebase-storage @@ 16.0.2:3435) com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage @@ 16.0.2:65) com.google.firebase.storage.obfuscated.zzc.zza(com.google.firebase:firebase-storage @@ 16.0.2:57) com.google.firebase.storage.zzc.run(com.google.firebase:firebase-storage @@ 16.0.2:68) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636) 在java.lang.Thread.run(Thread.java:764)

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)

也将文件上传到正确的位置,但错误仍然存​​在,并且无法将该下载URL放置到数据库中

Also the file is uploaded to the right place but the error continues and can't place that downloadurl to the database

我压缩了代码并删除了数据库部分,仍然是同样的问题

I shrunk the code and removed the database part and still the same issue

 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吗?

Also, there is no addOnProgressUpdate like the old storage implementation ?

推荐答案

问题是由您调用getDownloadUrl()引起的:

The problem is caused by your call to getDownloadUrl():

return mStorageReference.getDownloadUrl();

我的猜测是mStorageReference指向您的Cloud Storage存储桶的根,因此您要求提供整个存储桶的下载URL,这是不允许的.

My guess is that mStorageReference points to the root of your Cloud Storage bucket, so you're asking for the download URL of the entire bucket, which isn't allowed.

要解决此问题,关于您实际写到的StorageReference的下载URL:

To solve this, as for the download URL of the StorageReference you actually wrote to:

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();
    }
    ...

顺便说一句:我是通过从错误消息中搜索所需的开发人员凭据"来找到的] [

Btw: I found this by searching for the "Developer credentials required" from the error message](https://www.google.com/search?q=firebase+storage+"Developer+credentials+required").

这篇关于尝试将数据插入我的数据库的Firebase存储错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆