首页 > 解决方案 > iOS 应用程序:如何使用 kingfisher/sdwebimage 下载 aws s3 映像

问题描述

我想使用 kingfisher 在我的 swiftui 项目中的 collectionview 或列表中显示来自 aws s3 保护文件夹的图像。但是我找不到将图像显示为 aws 需要在请求中包含其他标头的解决方案。我尝试添加 Cheatsheet 中提到的自定义标题,但没有任何反应。

标签: swiftamazon-s3swiftuisdwebimagekingfisher

解决方案


您可以选择 Kingfisher 用于缓存的密钥。因此,创建一个预签名的 s3 url,然后在加载图像时使用您的 s3 密钥作为缓存,而不是完整的预签名 url。

            let getPreSignedURLRequest = AWSS3GetPreSignedURLRequest()
            getPreSignedURLRequest.bucket = media.bucket
            getPreSignedURLRequest.key = media.key
            getPreSignedURLRequest.httpMethod = .GET
            getPreSignedURLRequest.expires = Date(timeIntervalSinceNow: 3600)  // Change the value of the expires time interval as required
            AWSS3PreSignedURLBuilder.default().getPreSignedURL(getPreSignedURLRequest).continueWith { (task:AWSTask<NSURL>) -> Any? in
                if let error = task.error as NSError? {
                    print("Error: \(error)")
                    return nil
                }
                if let presignedURL = task.result {
                    DispatchQueue.main.async {
                        self.imageView.kf.indicatorType = .activity
                        let resource = ImageResource(downloadURL: URL(string: presignedURL.absoluteString!)!, cacheKey: media.key)
                        self.imageView.kf.setImage(with: resource)
                    }
                }
                return nil
            }

推荐阅读