所以我有这个小代码片段负责将图片上传到firebase存储。由于某些奇怪的原因,我的访问权限一直被拒绝。 storageRef似乎存在问题,因为它会进入if语句,打印出错误。
// will handle the sign up of a user
@objc func handleSignUp(){
// first we cant to take sure that all of the fields are filled
var profilePic: String = ""
// will take the user selected image and load it to firebase
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).PNG")
if let userImage = selectedImageFromPicker,let uploadData = UIImageJPEGRepresentation(userImage, 0.1){
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil{
print(error ?? "")
return
}
profilePic = (metadata?.downloadURL()!.absoluteString)!
guard let username = self.nameTextField.text,
let confirmPassword = self.confirmPasswordTextField.text,
let email = self.emailTextField.text,
let password = self.passwordTextField.text,
!username.isEmpty,
!email.isEmpty,
!password.isEmpty,
!confirmPassword.isEmpty
else {
print("Required fields are not all filled!")
return
}
if self.validateEmail(enteredEmail:email) != true{
let alertController = UIAlertController(title: "Error", message: "Please Enter A Valid Email", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
// will make sure user is validated before it even tries to create user
// will make sure the password and confirm password textfields have the same value if so it will print an error
if self.passwordTextField.text != self.confirmPasswordTextField.text {
let alertController = UIAlertController(title: "Error", message: "Passwords Don't Match", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
// will authenticate a user into the authentication services with an email and passowrd
AuthService.createUser(controller: self, email: email, password: password) { (authUser) in
guard let firUser = authUser else {
return
}
//will add user to the database
print(profilePic)
print(username)
UserService.create(firUser, username: username , profilePic: profilePic,location: self.userLocation!) { (user) in
guard let user = user else {
print("User successfully loaded into firebase db")
return
}
// will set the current user for userdefaults to work
print(user.profilePic)
print(user.username)
User.setCurrent(user, writeToUserDefaults: true)
// self.delegate?.finishSigningUp()
self.finishSigningUp()
}
}
})
}
}
错误是
Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission to access gs://eventful-3d558.appspot.com/profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG." UserInfo={object=profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG, bucket=eventful-3d558.appspot.com, ResponseBody={
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
如果有人知道它是什么我会很感激你的帮助。它阻止我注册用户。
答案 0 :(得分:4)
您必须将数据库规则设置为接受所有读取和写入数据库 - >规则
{
"rules": {
".read": true,
".write": true
}
}
通过将数据库读写规则设置为true,可以设置允许的所有读写请求。
如果您在Firebase存储中发现了一些权限错误,请更改Firebase存储 - >规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
答案 1 :(得分:0)
请务必检查Firebase身份验证。
Firebase存储分区需要Firebase身份验证才能上传文件。
答案 2 :(得分:0)
这是一个完美的firebase演示文稿,可以帮助解决这个问题:https://firebase.google.com/docs/database/security/