我在使用预签名网址和 AFNetworking 3.0
从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest
和 NSURLSession
获取图像,但是当我使用 AFHTTPSessionManager
和相同的 NSMutableURLRequest 时,我收到 403 错误
。 (我实际上想通过调用 setImageWithURLRequest 来加载图像,这也会给我一个 403 错误,但我编写了这段代码,使用 AFHTTPSessionManager
来尝试调试问题。)我能做什么AFNetworking
出错?
let request = NSMutableURLRequest()
request.HTTPMethod = "GET"
request.URL = coverPhotoUrl // A presigned url to an image on S3
// The operation succeeds if I uncomment the NSURLSession line
// and comment out the AFHTTPSessionManager line.
//NSURLSession.sharedSession().dataTaskWithRequest(request) {
AFHTTPSessionManager().dataTaskWithRequest(request) {
date, response, error in
if let error = error {
Log.error?.message("request failed: \(error.localizedDescription)")
} else {
Log.debug?.message("request succeeded.")
}
}
.resume()
最佳答案
由于内容类型问题,AFNetworking 请求失败。在我将 S3 文件的内容类型从二进制/八位字节更改为图像/jpeg 后,setImageWithURL 起作用了。
让 S3 设置内容类型也很困难。为了防止其他人使用,S3 会忽略我的 PUT 请求中的内容类型,直到我指定内容长度。尽管实际文件要长得多,但在生成预签名 URL 时,我只是使用了虚拟长度 1。
关于swift - 使用 NSURLSession 时 GET 成功,但使用 AFHTTPSessionManager 时 GET 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009149/