PUT NSData 从 S3 到 PreSigned URL,Alamofire.upload(...) 在 iOS 8 上不起作用

Posted

技术标签:

【中文标题】PUT NSData 从 S3 到 PreSigned URL,Alamofire.upload(...) 在 iOS 8 上不起作用【英文标题】:PUT NSData to PreSigned URL from S3 with Alamofire.upload(...) not working on iOS 8 【发布时间】:2016-04-13 12:36:13 【问题描述】:

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

连接=关闭; “内容类型”=“应用程序/xml”;日期=“星期三, 2016 年 4 月 13 日 12:19:21 GMT";服务器 = AmazonS3;...

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;

【问题讨论】:

【参考方案1】:

这个人引导我回答:Other guy having similar issue.

事实证明,Alamofire 中管理器会话上的 HTTPAdditionalHeaders 具有我之前调用的标头,而 Amazon S3 在 iOS 8 上不喜欢这样。

因此,我只需要在使用 .upload(...) 函数之前清除标题。

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

【讨论】:

截至 3/17,当前语法为 Alamofire.SessionManager.default.session.configuration.httpAdditionalHeaders = [:]

以上是关于PUT NSData 从 S3 到 PreSigned URL,Alamofire.upload(...) 在 iOS 8 上不起作用的主要内容,如果未能解决你的问题,请参考以下文章