Download Image Using Alamofire Using PUT

I'm currently trying to upload an image to amazon with a pre-signed URL.

How it works, I make a GET request to get a pre-signed URL and a PUT request to load the image using the URL returned from the GET request.

Two rules: Content-Type must be image \ jpeg, and http methods must be PUT.

So, currently my download code returns 200, but amazon rejects the data.

Here is my code:

Actual URL returned for download: 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-Signedhbcdb7fb8b8f8b8b8f8b8b8bf8b8bf8b8f8b8bf8f8b8f8b8f8b8bf9f8f8f8f8f8f9f9f9f9f9f9f9f9f9f9f9f9cf9f9f9f9f9f9f9f9f9f9f9f9f9f9f9bf9f9

                    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)
                        }
                    })

I suspect that when I print the http headers, the Content-Type always shows multipart / form-data, and not the / jpeg image I need, but I have now lost what to do to solve this problem.

+4
source share
2 answers

( , 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
        }

       let publicUrl = presignedUrl.components(separatedBy: "?")[0]
    }
+3

, ?

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)
            })
+1

Source: https://habr.com/ru/post/1668609/


All Articles