我正在尝试上传图像,然后将所述图像的下载位置存储在 val 中。昨天它工作 100% 完美,没有任何问题。我没有更改任何代码,现在无法上传图像,现在它说
Failed to Upload Image User does not have permission to access this object.
我检查了每个具有相同主题的 stack-overflow/Reddit 帖子,他们都说将我的规则的权限更改为公开。
我的规则设置为公开
service firebase.storage {
match /b/savephoto-a1cc3.appspot.com/o {
match /{allPaths=**} {
allow read, write;
}
}
}
这是上传图片的实现。
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}")
}
}
仍然不确定为什么我会得到一个
Failed to Upload Image User does not have permission to access this object.
即使规则设置为公开,并且我的代码看起来也完全没问题。 昨天正确上传图像,但现在出现此错误。
最佳答案
对存储桶名称进行硬编码并不是一个好主意。使用变量占位符作为存储桶名称,请参阅 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.
将您的存储规则更改为以下规则以实现您想要的效果。
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
关于android - 上传图像,Firebase 显示 "User does not have permission to access this object",即使存储设置是公开的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839832/