简体   繁体   English

无法在 Firebase 存储上获取上传图片的网址

[英]Not able to get the url of uploaded image on firebase storage

class FCMStorage {

var storage: Storage!

init() {
    storage = Storage.storage()
}

func storeImage(data: Data?, name: String, completion: @escaping ((String?, Error?)->Void)) {

    guard let data = data else {
        return
    }
    let metaData = StorageMetadata()
    metaData.contentType = "image/jpeg"
    let path = "images/" + name
    print("img to store = \(path)")
    let ref = storage.reference()
    let uploadTask = ref.child(path).putData(data, metadata: metaData) { (metadata, error) in
        
        if error == nil {
            print(metadata as Any)
            ref.downloadURL(completion: { (url, error) in
                completion(url?.absoluteString, error)
            })
        } else {
            print("storeImage error: \(error!)")
        }

    }
    
    uploadTask.observe(.progress) { (snapshot) in
        print((snapshot.progress?.fractionCompleted ?? 0) * 100.0)
        if snapshot.status == .success || snapshot.status == .failure {
            uploadTask.removeAllObservers(for: .progress)
        }
    }
 }
}

Using the able class I am able to upload the image on firebase successfully and I am able to see the uploaded image on the firebase too but...使用able类,我可以成功地在firebase上上传图像,我也可以在firebase上看到上传的图像,但是......

When I call downloadURL() method it always giving me the following error当我调用downloadURL()方法时,它总是给我以下错误

ref.downloadURL(completion: { (url, error) in
   completion(url?.absoluteString, error)
})

Error: Failed to retrieve a download URL.错误:无法检索下载 URL。

Anyone could help me out on this issue!!任何人都可以帮助我解决这个问题!!

EDIT编辑

When I print metadata of the file it prints the following....当我打印文件的元数据时,它会打印以下内容....

FIRStorageMetadata 0x283f99860: { FIRStorageMetadata 0x283f99860:{

bucket = "sportoilic.appspot.com";

contentDisposition = "inline; filename*=utf-8''8E13A816-FAF1-47ED-8F84-94BBB8C4C77F";

contentEncoding = identity;

contentType = "application/octet-stream";

generation = 1601287237056536;

md5Hash = "VgMH6NMPGJT//LCD8goaDA==";

metageneration = 1;

name = "8E13A816-FAF1-47ED-8F84-94BBB8C4C77F";

size = 114787;

timeCreated = "2020-09-28T10:00:37.056Z";

updated = "2020-09-28T10:00:37.056Z";

}

and after that when I try to get the download url for the uploaded image< I am getting the above mentioned error(Error: Failed to retrieve a download URL).之后,当我尝试获取上传图像的下载网址时<我收到上述错误(错误:无法检索下载网址)。

What is the issue?问题是什么? Am I missing something here?我在这里错过了什么吗?

After trial and error of several hours, finally got the solution.经过几个小时的反复试验,终于得到了解决方案。

class FCMStorage {

var ref: StorageReference!

init(path: String) {
    ref = Storage.storage().reference(withPath: path)
    print("img to store at path = \(path)")
}

func storeImage(data: Data?, completion: @escaping ((String?, Error?)->Void)) {

    guard let data = data else {
        return
    }
    
    let uploadTask = ref.putData(data, metadata: nil) { (metadata, error) in
        
        if error == nil {
            print(metadata as Any)
            
            self.ref.downloadURL(completion: { (url, error) in
                completion(url?.absoluteString, error)
            })
        } else {
            print("storeImage error: \(error!)")
        }

    }
    
    uploadTask.observe(.progress) { (snapshot) in
        print((snapshot.progress?.fractionCompleted ?? 0) * 100.0)
        if snapshot.status == .success || snapshot.status == .failure {
            uploadTask.removeAllObservers(for: .progress)
        }
    }
  }
}

So store image just need to call like this...所以存储图像只需要像这样调用......

  let path = "images/" + UUID().uuidString
    FCMStorage(path: path).storeImage(data: data) { (imgUrl, error) in
                
          if let strUrl = imgUrl, error == nil {
             FCMDatabase.init(OfTable: .chatRooms).setImageUrl(strUrl: strUrl, teamId: self.team?.id ?? 0, forMsgId: self.arrChats.last?.messageId ?? "") { (isDone) in
                        
                if isDone {
                         print("Image url set in database")
                        }
                    }
                } else {
                    print("Error: \(error?.localizedDescription ?? "Error while getting image url")")
                }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM