0
votes

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 ?

1
Can you add the complete stack trace to the question? I'd like to see if it comes from putFile. If it does, you should be able to reproduce the problem without any code related to Firebase, which would reduce the surface area we need to consider.Frank van Puffelen
for sure puf, let me edit the question @FrankvanPuffelenGastón Saillén
I have read the error code from here tools.ietf.org/html/rfc7231#section-6.5.3 but I think there is something to be with authenticating my client to write or something like thatGastón Saillén
All dependencies and google play services plugin are up to date, also tried enabling anonymous authenticationGastón Saillén
OK. That definitely seems to come from Storage. Can you remove/comment out the database code, and see if you still have the same problem? If so, please update the code in your question to reflect (and become a lot smaller).Frank van Puffelen

1 Answers

1
votes

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