成功上传文件后,来自 Firebase 存储的 downloadURL 为零
Posted
技术标签:
【中文标题】成功上传文件后,来自 Firebase 存储的 downloadURL 为零【英文标题】:downloadURL from Firebase Storage is nil after successfully uploading a file 【发布时间】:2019-07-08 13:00:08 【问题描述】:我是 firebase 新手,在这里遇到了问题。
我已经成功地将我的图片上传到了 firebase 存储,但是,我需要立即获取下载网址并将其发布到数据库中。而且我不确定为什么它在该闭包中不起作用。
以下是我的代码:-
func CreateNewChatRoom(user: User, caption: String, data: Data)
let filePath = "\(user.uid)/\(Int(NSDate.timeIntervalSinceReferenceDate)).jpg"
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
let reference = storageRef.child(filePath)
reference.putData(data, metadata: metaData) (metadata, error) in
if let error = error
print("\(error.localizedDescription)")
return
reference.downloadURL(completion: (url, error) in
if let error = error
print("Error : \(error.localizedDescription)")
return
else
if let downloadURL = url?.absoluteString
let idRoom = self.BASE_REF.child("rooms").childByAutoId()
idRoom.setValue(["caption":caption, "thumbnailUrlFromStorage": self.storageRef.child(metadata!.path!).description,"fileUrl": downloadURL])
)
【问题讨论】:
那么你得到了什么结果?您的代码是否正确获取了网址? 我得到 fileUrl = nil,但我不确定它为什么不执行闭包块。我检查了我的 Firebase 存储,图片已上传。 我不断从 xcode 控制台收到此错误消息:BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C6.1:2][0x7fbcf8d25db0] get output frames failed, state 8196 如果你在 if let 的开头做了一个 print(downloadURL),你看到 downloadURL 的值 do you have nil 吗? 【参考方案1】:我有同样的问题,我找到了以下解决方案, 我希望它对某人有用。 斯威夫特 5
导入 FirebaseAuth 导入 Firebase 存储 导入 FirebaseFirestore
typealias AddStoryResult = (Bool, Error?) -> Void typealias UploadImageResult = (StorageMetadata?, Error?) -> Void
类 StoryFirebaseAPI
static let storage = Storage.storage()
static var downloadUrlString: URL?
var image: UIImage?
let photoUri = "smth uri path"
uploadImage(image, photoUriString: photoUri) (metadata: StorageMetadata?, error: Error?) in
guard let metadata = metadata else
if let error = error
print("Error occured: \(error)")
completion(false, error)
return
// and at this point I have print(downloadUrlString)
completion(true, nil)
扩展 StoryFirebaseAPI
func uploadImage(_ image: UIImage, photoUriString: String, completion: @escaping UploadImageResult)
guard let imageData = image.jpegData(compressionQuality: 0.5) else
print("smth error")
completion(nil, nil)
return
let storageRef = storage.reference().child(photoUriString)
let imageMetadata = StorageMetadata()
imageMetadata.contentType = "image/jpeg"
storageRef.putData(imageData, metadata: imageMetadata) (storageMetadata, error) in
if let error = error
print("Error: \(error.localizedDescription)")
completion(nil, error)
return
guard let unwrappedMetadata = storageMetadata else
print("smth error")
print("Error: \(error?.localizedDescription ?? "")")
completion(nil, error)
return
storageRef.downloadURL url, error in
if let error = error
print("Error: \(error.localizedDescription)")
completion(nil, error)
return
else
if let url = url
downloadUrlString = url
completion(unwrappedMetadata, nil)
print("Image uploaded successfully")
【讨论】:
以上是关于成功上传文件后,来自 Firebase 存储的 downloadURL 为零的主要内容,如果未能解决你的问题,请参考以下文章
Firebase存储将图像成功上传到Firebase存储后获得下载URL
IONIC - 如何显示来自firebase存储的图像而无需下载它[关闭]
Vue 显示来自 firebase 云存储 blob 的图像