简体   繁体   English

iOS:下载AWS S3映像并将其缓存以备将来使用

[英]iOS: Downloading the AWS S3 images & cache it for future use

I have been pulling my hair out from Morning on trying to download the images (webp image) from Amazon S3 bucket and cache it. 我试图从Amazon S3存储桶下载图像(webp图像)并将其缓存,从早晨开始拔我的头发。 Whatever the approach I took, I have hit some or the other road block. 无论采取什么方法,我都遇到了一些或其他路障。 Different ways I tried to achieve this, 我试图实现这一目标的不同方式,

  1. Subclassing NSURLProtocol & Using the SDWebImage to retrieve the image as shown in the blog post here . 子类NSURLProtocol和使用SDWebImage检索图像,如图博客文章在这里 The problem with this approach is that, the canInitWithRequest method of the subclassed protocol will never get called even tough I have registered my subclassed NSURLProtocol class. 这种方法的问题在于,子类协议的canInitWithRequest方法永远不会被调用,即使很难,我已经注册了我的子类NSURLProtocol类。
  2. Planned to use the SDWebImage and other libraries directly, but we can't do that since there is no provision to set the Authorization headers in SDWebImage, even tough if we try setting it, SDWebImage will ignore it while creating an NSMutableURLRequest for downloading. 计划直接使用SDWebImage和其他库,但我们不能这样做,因为没有规定在SDWebImage中设置Authorization标头,即使我们尝试设置它也很难,SDWebImage会在创建NSMutableURLRequest下载时忽略它。

So right now I am left with an option either to use the Amazon SDK to download the image and then cache it using the SDWebImage's SDImageCache independently, or I need to add the SDWebImage source code to my project and then I need to modify it's source to take the authorization header. 所以现在我可以选择使用Amazon SDK下载图像,然后使用SDWebImage的SDImageCache独立缓存它,或者我需要将SDWebImage源代码添加到我的项目中,然后我需要将其源代码修改为采取授权标题。

Please let me know if there's any better way to achieve this, it'd be great if I could some pointers on what'd be the best way to achieve what I am trying to do. 请告诉我,如果有更好的方法可以实现这一目标,那么如果我能够指出什么是实现我想要做的最佳方式,那就太棒了。

Thanks in advance. 提前致谢。

To be honest, I looked into this myself for a long time and was unsatisfied with the built in caching system offered by AWS. 说实话,我自己研究了很长时间,并且对AWS提供的内置缓存系统不满意。 Instead, I chose to use Kingfisher https://github.com/onevcat/Kingfisher since it is built in swift and well used. 相反,我选择使用Kingfisher https://github.com/onevcat/Kingfisher,因为它内置于swift并且使用得很好。 I highly recommend it because it is highly customizable but also works perfectly "out of the box" 我强烈推荐它,因为它是高度可定制的,但也完美地“开箱即用”

You can choose what key Kingfisher uses for the cache. 您可以选择Kingfisher用于缓存的关键字。 So, create a presigned s3 url, then when loading the image use your s3 key as the cache rather then the full presigned url. 因此,创建一个预先签名的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
            }

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

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