Gastón Saillén
Gastón Saillén

Reputation: 13129

Firebase Storage bug trying to insert data into my database

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

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

and this one

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Now I tried this one too

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

and still having the same problem.

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

    }

Stacktrace of the error

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)

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

enter image description here

Edit

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

Image:

enter image description here

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

Upvotes: 0

Views: 281

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

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

return mStorageReference.getDownloadUrl();

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.

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").

Upvotes: 1

Related Questions