上传到AWS服务器上传失败?

Upload to AWS Server Upload Fails?

我正在尝试将照片上传到 AWS S3,我已按照所有步骤进行操作。一旦用户 select 来自 UIImagePickerController 的照片,代码就会被触发。然后代码将该图像上传到 AWS S3 数据库。然而,上传完成后出现此错误

Error Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1 "(null)" UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type=application/xml, Date=Wed, 27 Feb 2019 08:33:44 GMT, x-amz-request-id=B019306141A5FD4E, x-amz-id-2=nWHJCqbpTUAbNI65pNAIxzl8qXOn6y2jNg16CbvUCH/4wtqCYhVBqh86ZN6OYIV1pROSlnUA220=, x-amz-bucket-region=ap-southeast-1})

我不知道如何解决这个问题,如能成功上传照片,我们将不胜感激。

谢谢

代码

 @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    let imageAWSName = "ios_123.jpg" //+ NSUUID().uuidString + ".jpg"

    let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageAWSName)

    if !FileManager.default.fileExists(atPath: localPath!.path) {
        do {
            //try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
            try image.jpegData(compressionQuality: 1.0)?.write(to: localPath!)
            print("file saved")
        }catch {
            print("error saving file")
        }
    }
    else {
        print("file already exists")
    }
     //    let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: “your identity pool id”) //    let configuration = AWSServiceConfiguration(region: .APSoutheast1, credentialsProvider: credentialProvider) //    AWSServiceManager.default().defaultServiceConfiguration = configuration

    let s3BucketName = "adnexio-directory"
    //let compressedImage = image.resizableImage(withCapInsets: CGSize(width: 80, height: 80))
    let data: Data = image.pngData()!
    let remoteName = "IOSTEST.png"
    print("REMOTE NAME : ",remoteName)

    let expression = AWSS3TransferUtilityUploadExpression()
    expression.progressBlock = { (task, progress) in
        DispatchQueue.main.async(execute: {
            // Update a progress bar
            print("progress: ")
            print(progress)
        })
    }

    var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
    completionHandler = { (task, error) -> Void in
        DispatchQueue.main.async(execute: {
            print("error: ")
            print(error)
            // Do something e.g. Alert a user for transfer completion.
            // On failed uploads, `error` contains the error object.
        })
    }

    let transferUtility = AWSS3TransferUtility.default()
    transferUtility.uploadData(data, bucket: s3BucketName, key: remoteName, contentType: "image/png", expression: expression, completionHandler: completionHandler).continueWith{ (task) -> Any? in

        if let error = task.error {
            print("Error : \(error.localizedDescription)")
        }

        if task.result != nil {
            let url = AWSS3.default().configuration.endpoint.url
            let publicURL = url?.appendingPathComponent(s3BucketName).appendingPathComponent(remoteName)
            if let absoluteString = publicURL?.absoluteString {
                // Set image with URL
                print("Image URL : ",absoluteString)
            }
        }

        return nil


    }


    picker.dismiss(animated: true, completion: nil)

    }

完整错误报告

REMOTE NAME :  IOSTEST.png
Image URL :  https://s3.us-east-2.amazonaws.com/adnexio-directory/IOSTEST.png
2019-02-27 16:33:33.867505+0800 Adnexio[3854:1137756] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0400 / Completed: 131072 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0499 / Completed: 163840 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0599 / Completed: 196608 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0599 / Completed: 196608 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0599 / Completed: 196608 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0599 / Completed: 196608 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0699 / Completed: 229376 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0799 / Completed: 262144 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0899 / Completed: 294912 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.0999 / Completed: 327680 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1099 / Completed: 360448 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1199 / Completed: 393216 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1298 / Completed: 425984 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1398 / Completed: 458752 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1498 / Completed: 491520 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1598 / Completed: 524288 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1798 / Completed: 589824 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1798 / Completed: 589824 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1898 / Completed: 622592 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.1998 / Completed: 655360 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2097 / Completed: 688128 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2197 / Completed: 720896 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2397 / Completed: 786432 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2397 / Completed: 786432 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2497 / Completed: 819200 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2697 / Completed: 884736 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2697 / Completed: 884736 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2797 / Completed: 917504 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2996 / Completed: 983040 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.2996 / Completed: 983040 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3096 / Completed: 1015808 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3196 / Completed: 1048576 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3296 / Completed: 1081344 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3396 / Completed: 1114112 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3596 / Completed: 1179648 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3596 / Completed: 1179648 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3696 / Completed: 1212416 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3795 / Completed: 1245184 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.3895 / Completed: 1277952 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4095 / Completed: 1343488 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4095 / Completed: 1343488 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4195 / Completed: 1376256 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4395 / Completed: 1441792 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4395 / Completed: 1441792 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4495 / Completed: 1474560 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4594 / Completed: 1507328 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4694 / Completed: 1540096 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4894 / Completed: 1605632 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.4894 / Completed: 1605632 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5094 / Completed: 1671168 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5094 / Completed: 1671168 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5194 / Completed: 1703936 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5294 / Completed: 1736704 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5393 / Completed: 1769472 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5593 / Completed: 1835008 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5693 / Completed: 1867776 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5693 / Completed: 1867776 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5793 / Completed: 1900544 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5993 / Completed: 1966080 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.5993 / Completed: 1966080 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6093 / Completed: 1998848 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6292 / Completed: 2064384 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6292 / Completed: 2064384 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6392 / Completed: 2097152 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6592 / Completed: 2162688 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6692 / Completed: 2195456 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6692 / Completed: 2195456 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6792 / Completed: 2228224 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6991 / Completed: 2293760 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.6991 / Completed: 2293760 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7091 / Completed: 2326528 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7291 / Completed: 2392064 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7291 / Completed: 2392064 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7491 / Completed: 2457600 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7491 / Completed: 2457600 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7591 / Completed: 2490368 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7691 / Completed: 2523136 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7890 / Completed: 2588672 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7890 / Completed: 2588672 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.7990 / Completed: 2621440 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8090 / Completed: 2654208 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8190 / Completed: 2686976 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8290 / Completed: 2719744 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8390 / Completed: 2752512 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8490 / Completed: 2785280 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8590 / Completed: 2818048 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8689 / Completed: 2850816 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8789 / Completed: 2883584 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8889 / Completed: 2916352 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.8989 / Completed: 2949120 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9089 / Completed: 2981888 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9189 / Completed: 3014656 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9389 / Completed: 3080192 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9389 / Completed: 3080192 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9588 / Completed: 3145728 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9588 / Completed: 3145728 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9688 / Completed: 3178496 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9888 / Completed: 3244032 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 0.9888 / Completed: 3244032 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 3280784 of 3280784  
progress: 
<NSProgress: 0x28326df40> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 3280784 of 3280784  
error: 
Optional(Error Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=1 "(null)" UserInfo={Server=AmazonS3, Transfer-Encoding=Identity, Connection=close, Content-Type=application/xml, Date=Wed, 27 Feb 2019 08:33:44 GMT, x-amz-request-id=B019306141A5FD4E, x-amz-id-2=nWHJCqbpTUAbNI65pNAIxzl8qXOn6y2jNg16CbvUCH/4wtqCYhVBqh86ZN6OYIV1pROSlnUA220=, x-amz-bucket-region=ap-southeast-1})
2019-02-27 16:34:33.514453+0800 Adnexio[3854:1137756] TIC Read Status [5:0x283670300]: 1:57
2019-02-27 16:34:33.514611+0800 Adnexio[3854:1137756] TIC Read Status [5:0x283670300]: 1:57
2019-02-27 16:34:33.514686+0800 Adnexio[3854:1137756] TIC Read Status [5:0x283670300]: 1:57
2019-02-27 16:34:33.517102+0800 Adnexio[3854:1137756] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C5.1:2][0x11be38fa0] get output frames failed, state 8196
2019-02-27 16:34:33.517420+0800 Adnexio[3854:1137756] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C5.1:2][0x11be38fa0] get output frames failed, state 8196
2019-02-27 16:34:33.518561+0800 Adnexio[3854:1137756] TIC Read Status [5:0x0]: 1:57
2019-02-27 16:34:33.518644+0800 Adnexio[3854:1137756] TIC Read Status [5:0x0]: 1:57
2019-02-27 16:34:33.518712+0800 Adnexio[3854:1137756] TIC Read Status [5:0x0]: 1:57
2019-02-27 16:34:33.518786+0800 Adnexio[3854:1137756] TIC Read Status [5:0x0]: 1:57
2019-02-27 16:34:46.805048+0800 Adnexio[3854:1137832] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C6.1:2][0x11bf10660] get output frames failed, state 8196
2019-02-27 16:34:46.805277+0800 Adnexio[3854:1137832] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C6.1:2][0x11bf10660] get output frames failed, state 8196
2019-02-27 16:34:46.805746+0800 Adnexio[3854:1137832] TIC Read Status [6:0x0]: 1:57
2019-02-27 16:34:46.805802+0800 Adnexio[3854:1137832] TIC Read Status [6:0x0]: 1:57
2019-02-27 16:34:46.875852+0800 Adnexio[3854:1138085] TIC Read Status [7:0x283662400]: 1:57

其他人发布了此问题,并表示他们通过检查 AWS Cognito 和 S3 服务器是否位于同一区域来修复问题。我的一开始也不在同一个地区,但我修复了它,错误仍然存​​在。

我犯了一个愚蠢的错误,

AWS 由于不一致而阻止访问。此处未显示,但在 App Delegate 中显示,我对该区域的配置是 MisMatched

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.APSoutheast1,
                                                            identityPoolId:"XXXX")

而我的

let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider) 

当我把它改成 APSoutheast1

一切正常。

如果您在 credentialsProvider 和配置中使用了错误的区域,则可能会出现此错误。只需使用错误提及的区域对我有用。

x-amz-bucket-region=ap-southeast-1 // use ap-southeast-1 as region