相关文章推荐
好帅的苦瓜  ·  Vue ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm currently trying to upload an Image to amazon with pre-signed url.

The way it works is, I make a GET request to get the pre-signed URL and than a PUT request to upload the image by using the url returned from the GET request.

The two rules are: the Content-Type needs to be image\jpeg and the http methods must be PUT.

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

Here is my code:

The actual url returned for the upload is: 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)

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

I actually recently had the same requirements (except I needed PNG instead of JPG).

Here is how you go about it.

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)
   let imageData = UIImageJPEGRepresentation(image  , 0.7)
      Alamofire.upload(imageData!, to: url, method: .put, headers: nil).responseJSON(completionHandler: { (response) in
                debugPrint(response)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.