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")
}, 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.
Bruno source
share