0
votes

In my android app i am using Firebase Storage to save images i have set security rules to null then too i am not able to upload images and getting error. Below are my error ,security rules and android code.Please let me know what i did wrong in code.

Logcat error : StorageException has occurred. User does not have permission to access this object. Code: -13021 HttpResult: 403

Security rules:

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

Java code:

   galleryFab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

                if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){

                    ActivityCompat.requestPermissions(SetAvatar.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
                }
                else{

                    Intent i = new Intent(Intent.ACTION_PICK);
                    i.setType("image/*");
                    startActivityForResult(i,GALLERY_INTENT);
                }
            }
        }
    });

    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) {

    if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){

        uri = data.getData();
        cv.setImageURI(uri);
        final StorageReference filepath = sRef.child("Profile 
      Images").child(uri.getLastPathSegment());

        UploadTask uploadTask = filepath.putFile(uri);

        uploadTask.addOnSuccessListener(new 
     OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

       Toast.makeText(SetAvatar.this, "Upload 
                                     successful",Toast.LENGTH_SHORT).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                Toast.makeText(SetAvatar.this, "Upload Failed -> " + e, 
   Toast.LENGTH_SHORT).show();

            }
        });
      }
    }

Thanks

1
But i am unable to post image could you please explain.Digvijay
first make your request.auth == true then try againuser9025311
I have changed it to true but still getting error.But i am not authenticating users.Digvijay
Your security rules require the user to be authenticated. Since your code doesn't show that you authenticate the user in any way, it seems like the error message is correct: the user is not allowed to write to the storage location.Frank van Puffelen

1 Answers

0
votes

change your security rules to:

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

But this will make your Bucket to be modified by anyone and I don't recommend it. However, you may use it for testing purposes.