【问题标题】:Alamofire and Pre-signed url upload objectAlamofire 和预签名的 url 上传对象
【发布时间】:2017-01-26 10:24:35
【问题描述】:

目前我正在尝试使用 Alamofire 上传图片,来自 Amazon 的示例实现如下:http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html

.NET 版本代码:

private void UploadS3FileByPresignedUrl(string url, string localFile, string contentType)
    {
        using (var client = new HttpClient())
        {
            using (var stream = File.OpenRead(localFile))
            {
                //// Need to add contentType otherwise the StreamContent is "binary/octet-stream"
                var fileContent = new StreamContent(stream);
                fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
                var response = client.PutAsync(url, fileContent).Result;
                response.EnsureSuccessStatusCode();
            }

        }
    }

基本上我必须在两个条件下上传图片:

  • 只支持jpg图片文件。
  • 客户端获取上传URL后,需要使用内容类型为“image/jpeg”的HTTP PUT上传个人资料图片这里是.NET客户端的示例代码

我正在尝试通过使用 Alamofire 来做到这一点,但我没有成功...

我正在使用 swift 3 和 Alamofire 4,目前我有这个代码:

let image = UIImage(named: "Sky-Sunset")

        Alamofire.upload(multipartFormData:{ multipartFormData in
            multipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")},
                         usingThreshold:UInt64.init(),
                         to:".../my/profile/image/uploadurl",
                         method:.put,
                         headers:["Content-Type": "image/jpeg"],
                         encodingCompletion: { encodingResult in
                            switch encodingResult {
                            case .success(let upload, _, _):
                                upload.responseJSON { response in
                                    debugPrint(response)
                                }
                            case .failure(let encodingError):
                                print(encodingError)
                            }
        })

【问题讨论】:

    标签: ios amazon-web-services swift3 alamofire


    【解决方案1】:

    我建议使用适用于 iOS 的 AWS 开发工具包,对于预签名的 url 支持,您可以关注文档@https://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3-pre-signed-urls.html

    AWSS3PreSignedURLBuilder.default().getPreSignedURL(getPreSignedURLRequest).continueWith { (task:AWSTask<NSURL>) -> Any? in
        if let error = task.error as? NSError {
            print("Error: \(error)")
            return nil
        }
    
        let presignedURL = task.result
        print("Download presignedURL is: \(presignedURL)")
    
        Alamofire.upload(
           multipartFormData: { multipartFormData in
                 multipartFormData.append(unicornImageURL, withName: "unicorn")
                 multipartFormData.append(rainbowImageURL, withName: "rainbow")
           },
           to:presignedURL.absoluteString
           ...
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 2021-02-15
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2018-07-10
      相关资源
      最近更新 更多