【问题标题】:Firebase storage downloadUrl completion handler always returns errorFirebase 存储 downloadUrl 完成处理程序始终返回错误
【发布时间】:2018-05-28 09:39:43
【问题描述】:

所以我关注documentation,但我总是收到此错误:

出现错误:用户没有访问权限 gs://***-*****.appspot.com/(null)。

图片已成功上传。唯一的问题是,当调用完成处理程序时,它返回一个错误,我无法获取图像路径。

代码如下:

func uploadImage(_ image: UIImage, completion: @escaping (String?, Error?) -> Void) {
        let filename = "\(Date().timeIntervalSince1970)"
        let imageReference = storage.child(FirestoreStorage.dishImagesPath).child(filename)

        guard let imageData = UIImageJPEGRepresentation(image, 0.8) else {
            completion(nil, CommonError.imageConversionError)
            return
        }
        let metadata = StorageMetadata()
        metadata.contentType = "image/jpeg"

        imageReference.putData(imageData, metadata: metadata, completion: { [storage] (metadata, error) in
            storage.downloadURL(completion: { (url, error) in
                guard let url = url else {
                    completion(nil, error)
                    return
                }
                completion(url.absoluteString, nil)
            })
        })
    }

另外,这些是我的安全规则@Firebase Storage:

service firebase.storage {
   match /b/my-project.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

【问题讨论】:

    标签: swift firebase firebase-storage


    【解决方案1】:

    发生错误是因为您指向存储服务以获取下载 URL。

    storage.downloadURL
    

    您应该改为使用存储引用来获取它。

    imageReference.downloadURL
    

    【讨论】:

    • 我只是在回答我的问题,正是你的意思。我接受我的回答并支持你的回答。干杯!
    【解决方案2】:

    好的,所以我能够解决这个问题并在完成处理程序中获取图像路径。什么问题?就是这两行:

    imageReference.putData(imageData, metadata: metadata, completion: { [storage] (metadata, error) in
        storage.downloadURL(completion: { (url, error) in
    

    imageReference 是对图像本身的引用,storage 是对全局存储的引用。这种误解来自文档。所以它应该是这样的:

    imageReference.putData(imageData, metadata: metadata, completion: { (metadata, error) in
        imageReference.downloadURL(completion: { (url, error) in
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2019-03-15
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多