获取“用户无权访问 gs://xxxx.appspot.com/(null)”
升级 Firebase 后(Firebase 5.1.0(原为 4.11.0)、FirebaseStorage 3.0.0(原为 2.1.3))然后编写代码:
这是升级前的工作代码:
imageUploadTask.observe(.success) { snapshot in
let downloadURL = snapshot.metadata!.downloadURL()!.description
addPostData(userId: userId, downloadURL: downloadURL)
}
升级后的代码,因为现在不推荐使用 StorageMetaData 的 downloadURL:
imageUploadTask.observe(.success) { snapshot in
storageRef.downloadURL { (url, error) in
guard let downloadURL = url else { return }
addPostData(userId: userId, downloadURL: downloadURL.description)
}
在完成 block 中打印错误返回: “用户无权访问 gs://xxxx.appspot.com/(null)”
我尝试过的:(1) 检查用户是否已通过身份验证(Auth.auth().currentUser 不为零); (2) 从 snapshot.metadata.storageReference 获取 storageRef 没有解决这个问题。
我想继续只允许经过身份验证的用户上传图片,并且不希望更改 Firebase 存储规则。
有什么想法吗?
最佳答案
我有同样的问题,解决方案(在重新检查 guide doc 中的代码示例后)是使用代表图像文件的 Child 对象来调用 downloadURL
方法。看下面的代码:
// Upload the image
let imagesFolder = Storage.storage().reference().child("images")
let imageFileName = NSUUID().uuidString + ".jpg"
let imageFileObject = imagesFolder.child(imageFileName)
imageUploadTask.observe(.success) { snapshot in
imageFileObject.downloadURL(completion: { (url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
guard let downloadURL = url else { return }
print("Url: \(downloadURL)")
addPostData(userId: userId, downloadURL: downloadURL.description)
})
}
关于swift - 用户无权访问 gs ://xxxx. appspot.com/(null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647828/