简体   繁体   English

如何使用 Alamofire 5 将图像作为多部分数据上传到 aws 预签名 url?

[英]How to upload image to aws pre signed url as multipart data using Alamofire 5?

I am trying to upload an image to the pre-signed AWS URL.我正在尝试将图像上传到预先签名的 AWS URL。 I tried with Alamofire 5 upload method which accepts multipartData.我尝试使用接受 multipartData 的 Alamofire 5 上传方法。 I am getting a 403 error with Alamofire 5 multipart upload.我在使用 Alamofire 5 分段上传时遇到 403 错误。

When I try with URLSession it is working properly.当我尝试使用 URLSession 时,它工作正常。

/// Working code

        var request: URLRequest = URLRequest(url: requestURL)
        request.httpMethod = "PUT"
        request.httpBody = image
        request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
        let tasksession: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (response, urlResp, error) in
            if let data: Data = response {
                do {
                    let json: [String: Any]? = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                    debugPrint("json \(json)")
                }
                catch {
                    debugPrint("error \(error)")
                }
            }



            print(response ?? "response nil")
            print(error ?? "response nil")
        })
        tasksession.resume()

Same when I try with AF 5 it is not working当我尝试使用 AF 5 时也一样,它不起作用

// Not working code. // 不工作的代码。

 AF.upload(multipartFormData: { (multiPart) in

            multiPart.append(imageData, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            }, to: url, method: .put, headers: ["Content-Type": "image/jpeg"],
                                                  ])
        .uploadProgress(queue: .main, closure: { progress in
                    //Current upload progress of the file
                    print("Upload Progress: \(progress.fractionCompleted)")
                })
                .responseJSON(completionHandler: { data in
                    let json: [String: Any]? = data as? [String: Any]
                    debugPrint("upload complete json \(data)")
            })

Could someone tell why it is not working with multipart upload with Alamofire 5, multipart upload is working in Android.有人可以告诉为什么它不能使用 Alamofire 5 进行分段上传,分段上传在 Android 中有效。

Alamofire Version 4.9.1 Alamofire 版本 4.9.1

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageData, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            //Any Post Params if you have.
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to:uploadUrlStr) //uploadUrlStr: upload url in your case
        { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (progress) in
                    print("Uploading")
                    print(CGFloat(progress.fractionCompleted * 100))
                })
                
                upload.responseJSON { response in
                    print("Upload Finished")
                    guard let resultValue = response.result.value else {
                        NSLog("Result value in response is nil")
                        return
                    }

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

This is working for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Alamofire将多部分图像上传到服务器 - How to upload multipart image to server using Alamofire Alamofire和预签名网址上传对象 - Alamofire and Pre-signed url upload object 我想使用 alamofire swift 5 将图像上传到多部分表单数据 - I want to upload image to multipart form data using alamofire swift 5 如何使用 Alamofire 5.0.0-beta.3 (Swift 5) 上传图像(多部分) - how to upload image (Multipart) using Alamofire 5.0.0-beta.3 (Swift 5) 如何在iOS,Swift3,Alamofire 4中使用多部分表单数据将图像作为参数上传以及其他参数 - How to upload image as a parameter, with other parameters using multipart form data in ios, swift3, Alamofire 4 如何使用alamofire图像检查分段上传是否成功 - How to check if the multipart upload was successful with alamofire image 如何使用Alamofire上传多部分图像? - How to upload multiple images in multipart using Alamofire? Alamofire Multipart图片上传失败 - Alamofire Multipart image upload failed 使用 Alamofire '~> 5.0.0-beta.3' 从 multipart-from-data 上传图像 - upload image from multipart-from-data using Alamofire '~> 5.0.0-beta.3' 在Swift 4中的Alamofire 4中使用multipart上传图像和多个参数 - upload image and multiple parameters using multipart in alamofire 4 in swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM