使用alamofire上传将图像上传到AWS S3

时间:2017-09-21 10:18:09

标签: ios swift amazon-web-services amazon-s3 alamofire

我正在尝试使用上传功能将jpeg图片上传到AWS。但我收到以下错误:

nw_endpoint_handler_add_write_request [29.1 52.92.250.6:443 failed socket-flow (satisfied)] cannot accept write requests

一些帮助会很有用。这是我正在使用的代码(params是Data类型)

let request = Alamofire.upload(params, to: url, method: .put, headers: ["Content-Type":"image/jpeg"])

        request.responseJSON { (responseJson) in
            switch responseJson.result {
            case .success:
                print("Success: \(responseJson.result.value)")
                break
            case .failure:
                print("Call failed: \(responseJson.result.value)")
                break
            default:
                print("____")
            }
        }

2 个答案:

答案 0 :(得分:1)

可能最好使用亚马逊自己的SDK来执行此操作。

http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transfermanager.html

答案 1 :(得分:0)

试试这个:

Swift 3.x:

 func uploadImageWith(parameter params:Dictionary<String,String>,image:UIImage?,handler:@escaping ((Dictionary<String,Any>?) -> Void)) {
       Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in params {
            multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
        }
        if image != nil {
            if let imgData = UIImageJPEGRepresentation(image!, 0.5) {
                multipartFormData.append(imgData, withName: "photo_upload", fileName: "file.png", mimeType: "image/png")
            }
        }
       }, to: "http://") { (encodingResult) in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                switch response.result {
                case .success:
                    if let jsonDict = response.result.value as? Dictionary<String,Any> {
                        print("Json Response: \(jsonDict)")
                        handler(jsonDict)
                        print(jsonDict,(response.response!.statusCode))
                    }
                    else{
                        print(response.response!.statusCode)
                        handler(nil)
                    }
                    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                        print("Server Response: \(utf8Text)") // original server data as UTF8 string
                    }
                    break
                case .failure(let error):
                    print(response.response!.statusCode)
                    print_debug(error)
                    handler(nil)
                    break
                }
            }
        case .failure(let encodingError):
            print(encodingError)
        }

        }
    }

<强>用法:

uploadImageWith(parameter: ["key":"value"], image: UIImage(named:"demo")) { (response) in
      if response != nil {
          print(response)
      } else {
          print("Something went wrong")
      }            
}