PUT NSData to PreSigned URL from S3 with Alamofire.upload(...) not working on iOS 8

PUT NSData to PreSigned URL from S3 with Alamofire.upload(...) not working on iOS 8

以下代码在 iOS9 上运行良好,但在 iOS8 上运行 运行 时失败,亚马逊返回错误 400。响应仅包含 headers:

NSData 是一张图片,而 Content-Type 是 "image/png",这告诉亚马逊不要将其存储为 "binary/octet-stream"。

func uploadFile(locationURL: String, http: Alamofire.Method, mimeType: String, fileData: NSData) -> ApiCaller {
    Alamofire.upload(http, locationURL, headers: ["Content-Type": mimeType], data: fileData)
        .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
            if let uploadProgress = self.uploadProgress {
                uploadProgress(bytesWritten, totalBytesWritten, totalBytesWritten);
            }
        }
        .response { (req, res, json, error) in
            self.returnResult(req, res: res, json: json, error: error, tag: 0)
            return();
        }
    return self;
}

这个人引导我找到答案:Other guy having similar issue.

事实证明,Alamofire 中管理器 session 上的 HTTPAdditionalHeaders 有 headers 来自我之前的调用,而 Amazon S3 在 iOS 上不喜欢这个 8.

因此,我所需要的只是在使用 .upload(...) 函数之前清除 headers。

Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = [:];