使用PUT上传Alamofire图像

问题描述 投票:5回答:2

我目前正在尝试使用预先签名的网址将图片上传到亚马逊。

它的工作方式是,我发出一个GET请求来获取预签名的URL,而不是PUT请求通过使用从GET请求返回的URL来上传图像。

这两个规则是:Content-Type需要是image \ jpeg,http方法必须是PUT。

所以,目前我的上传代码返回200但亚马逊拒绝数据。

这是我的代码:

为上传返回的实际网址是:https://mimik-apps-channel.s3-us-west-2.amazonaws.com/profiles/2312528782074206653.jpg?X-Amz-Expires=3600&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJ36SCZW7WGBAW7CQ/20170202/us-west-2/s3/aws4_request&X-Amz-Date=20170202T102202Z&X-Amz-SignedHeaders=host&X-Amz-Signature=007ad8694fe1ed83b08d4890f17b9985e169f7ab8fcd7b0d648a383c69ebc748

                    var headers = Alamofire.SessionManager.defaultHTTPHeaders
                    headers["Content-Type"] = "image/jpeg"
                    let URL = try! URLRequest(url: url, method: .put, headers: headers)

                    Alamofire.upload(multipartFormData: { (multipartFormData) in
                        let compressionQuality: CGFloat = 0.8
                        guard let imageData = UIImageJPEGRepresentation(image, compressionQuality) else {
                            print("Unable to get JPEG representation for image \(image)")

                            return
                        }

                        multipartFormData.append(imageData, withName: "image.jpg", mimeType: "image/jpeg")
                        // code
                    }, with: URL, encodingCompletion: { (result) in
                        switch result {
                        case .success(let upload, _, _):
                            upload.responseJSON { response in
                                print("SUCCESS -> \(response.request?.allHTTPHeaderFields)")

                            }

                        case .failure(let encodingError):
                            print(encodingError)
                        }
                    })

我怀疑当我打印http标题时,Content-Type总是显示multipart / form-data而不是我需要的image / jpeg,但是目前我对如何解决这个问题感到很遗憾。

ios image amazon-web-services amazon-s3 alamofire
2个回答
6
投票

我实际上最近有相同的要求(除了我需要PNG而不是JPG)。

这是你如何去做的。

let compressionQuality: CGFloat = 0.8
guard let imageData = UIImageJPEGRepresentation(image, compressionQuality) else {
    print("Unable to get JPEG representation for image \(image)")
    return
}

let headers = [
    "Content-Type": "image/jpeg"
]

// presignedUrl is a String

Alamofire.upload(imageData, to: presignedUrl, method: .put, headers: headers)
    .responseData {
        response in

        guard let httpResponse = response.response else {
             print("Something went wrong uploading")
             return
        }

        if let publicUrl = presignedUrl.components(separatedBy: "?").first { 
             print(publicUrl)
        }
}

2
投票

您是否更改了允许后台服务的功能?

enter image description here

对我有用的东西:

   let imageData = UIImageJPEGRepresentation(image  , 0.7)

      Alamofire.upload(imageData!, to: url, method: .put, headers: nil).responseJSON(completionHandler: { (response) in
                debugPrint(response)
            })
© www.soinside.com 2019 - 2024. All rights reserved.