2

I'm trying to upload an image, then have the download location of said image stored in a val. Yesterday it was working 100% perfectly with no issue. I changed no code and I cant upload the image now, now it says

Failed to Upload Image User does not have permission to access this object.

I checked every single stack-overflow/Reddit post with the same topic, and they all say to change permissions of my rules to public.

My rules are set to public

service firebase.storage {
  match /b/savephoto-a1cc3.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

This is the implementation of uploading the image.

      val ref = FirebaseStorage.getInstance().getReference("/images/profilePictures/$filename")

        //Stores File
        ref.putFile(selectedPhoto!!)
            .addOnSuccessListener {
                Log.d("main", "Succesfully Uploaded Image ${it.metadata?.path}")

                //Gets Download Link for Image
                ref.downloadUrl.addOnSuccessListener {
                    val downloadImg = it.toString()
                    Log.d("main", "Profile Picture Location $it")
                    //Create User
                    createUser(downloadImg, age, gender, firstName, lastName)
                }
            }
            .addOnFailureListener {
                Log.d("main", "Failed to Upload Image ${it.message}")
            }
    }

still unsure why I'm getting a

Failed to Upload Image User does not have permission to access this object.

even when the rules are set to public and my code seems perfectly fine. It was properly uploading images yesterday but now I'm being given this error.

Dennis Alund
  • 2,916
  • 1
  • 13
  • 34
  • is this "/b/savephoto-a1cc3.appspot.com/o" your path are you sure. I think you get the code from here https://stackoverflow.com/questions/38671444/user-does-not-have-permission-to-access-this-object-firebase-storage-android . So you need to check your application id from firebase – Ergin Ersoy Apr 24 '19 at 23:30

1 Answers1

2

Hard coding the bucket name is not a good idea. Use variable placeholder for the bucket name instead as see in the documentation.

Storage Security Rules must first specify the service (in our case firebase.storage), and the Cloud Storage bucket (via match /b/{bucket}/o) which rules are evaluated against. The default rules require Firebase Authentication, but here are some examples of other common rules with different access control.

Change your storage rules to the rules below to achieve what you want.

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

enter image description here

Dennis Alund
  • 2,916
  • 1
  • 13
  • 34