尝试将图像上传到AWS S3存储桶错误-1005

时间:2017-10-10 22:43:27

标签: swift amazon-web-services asynchronous amazon-s3 xcode9

我正在尝试在S3上传图片但没有成功。它总是向我发送-1005错误网络连接丢失。

  

2017-10-11 00:24:15.843988 + 0200 OOL [18620:712534]任务。< 4> HTTP加载失败(错误代码:-1005 [4:-4])   2017-10-11 00:24:15.844396 + 0200 OOL [18620:712782]任务。< 4>完成错误 - 代码:-1005   上传图片失败,显示错误网络连接丢失。

经过一些研究后我发现客户端 - 服务器时差可能存在问题,差异不必超过15分钟。如果这些信息属实,我怎么解决呢?

这是我的AppDelegate.swift

let awsCredentialsKeys = Helper.shared.getAWSCredentialKeys() //0 is the access key. 1 is the secret key.
let staticCredentials = AWSStaticCredentialsProvider(accessKey: awsCredentialsKeys.0 , secretKey: awsCredentialsKeys.1)
let configration = AWSServiceConfiguration(region: .EUWest2, credentialsProvider: staticCredentials)

AWSServiceManager.default().defaultServiceConfiguration = configration

这是我的上传请求代码:

///Save image to AWS S3 Bucket
func uploadImageToS3(filePath:URL, bucketName: String, imageKeyName: String) -> String? {

    var s3URL = String()
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest?.bucket = bucketName
    uploadRequest?.key = imageKeyName
    uploadRequest?.contentType = "image/jpeg"
    uploadRequest?.body = filePath
    uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.unknown
    uploadRequest?.uploadProgress = {
        (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
        DispatchQueue.main.async(execute: {
            print(totalBytesSent)
            print(totalBytesExpectedToSend)
        })
    }

    let transferManager = AWSS3TransferManager.default()
    transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread()) { (task:AWSTask) -> Any? in
        if let error = task.error as NSError? {
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code){
                switch code {
                case .cancelled, .paused:
                        break
                default:
                    print("Upload image failed with error \(error.localizedDescription)")
                }
            } else {
                print("Upload image failed with error \(error.localizedDescription)")
            }

        }
        if task.result != nil {
            s3URL = "https://\(Helper.shared.getS3BucketURL())/\(Helper.shared.getAWSBucketName())/\(imageKeyName)"
            print("Uploaded to: \(s3URL)")
        }
        return nil
    }

    return s3URL

}

0 个答案:

没有答案