AWS iOS SDK中的S3上载每次都失败

时间:2016-03-22 08:31:33

标签: amazon-s3 aws-sdk aws-sdk-ios

尝试将图像上传到s3,但每次都因为未知原因而失败。

private func uploadImageToS3(withS3Path s3path: String, image: UIImage, progress: (Float -> Void)?) -> BFTask{
    let path: NSString = NSTemporaryDirectory().stringByAppendingString("profile_image.jpg")
    let imageData = UIImageJPEGRepresentation(image, 0.8)
    imageData?.writeToFile(path as String, atomically: true)

    let url: NSURL = NSURL(fileURLWithPath: path as String)

    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest.bucket = "my_bucket"
    uploadRequest.key = s3path
    uploadRequest.contentType = "image/jpg"
    uploadRequest.body = url

    let transferManager = CognitoCredentialProvider.sharedInstance.s3TransferManager
    let completionSource = BFTaskCompletionSource()
    transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {(task:AWSTask!) in
        if task.error != nil{
            completionSource.setError(task.error!)
            return nil
        }else{
            completionSource.setResult(s3path)
        }
        return nil
    })
    return completionSource.task
}

上传开始但在短时间后失败。日志:

  

进展0.126221

     

进展0.252442

     

进展0.378663

     

进展0.504884

     

AWSiOSSDKv2 [错误] AWSURLSessionManager.m行:260 | - [AWSURLSessionManager URLSession:task:didCompleteWithError:] |会话任务失败并显示错误:错误域= NSURLErrorDomain代码= -1017“无法解析响应”UserInfo = {NSUnderlyingError = 0x7fda2f90e3c0 {错误域= kCFErrorDomainCFNetwork代码= -1017“(null)”UserInfo = {_ kCFStreamErrorCodeKey = -1,_kCFStreamErrorDomainKey = 4 },NSErrorFailingURLStringKey = https://s3-us-west-2.amazonaws.com/my_bucket/profile_image/cognitoID/profile_image.jpg,NSErrorFailingURLKey = https://s3-us-west-2.amazonaws.com/my_bucket/profile_image/cognitoID/profile_image.jpg,_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1,NSLocalizedDescription =无法解析响应}

传输管理器初始化:

 let kclIdentityProvider = KCLCognitoIdentityProvider(regionType: .USEast1, identityPoolId: "poolID", userSession: UserSession.sharedInstance)
    let cognitoCredProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityProvider: kclIdentityProvider, unauthRoleArn: nil, authRoleArn: nil)
    self.credProvider = cognitoCredProvider
    let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: cognitoCredProvider)
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

    // s3, sns use uswest2
    let usWest2 = AWSServiceConfiguration(region: AWSRegionType.USWest2, credentialsProvider: self.credProvider)
    AWSS3TransferManager.registerS3TransferManagerWithConfiguration(usWest2, forKey: "USWest2S3")
    AWSSNS.registerSNSWithConfiguration(usWest2, forKey: "USWest2SNS")

    self.snsClient = AWSSNS(forKey: "USWest2SNS")
    self.s3TransferManager = AWSS3TransferManager.S3TransferManagerForKey("USWest2S3")

我可以以某种方式获得有关上传失败原因的更多信息吗?

1 个答案:

答案 0 :(得分:0)

代码-1017表示服务器没有响应有效的HTTP响应。打开详细日志记录以获取更多信息:

    AWSLogger.defaultLogger().logLevel = .Verbose

我使用以下内容上传到S3:

import AWSS3

/** Schedule an upload request to S3 for a UIImage
    Parameter imageURL: NSURL to image location on the device
    Parameter imageName: string with image name
    Parameter imageFile: UIImage to be upload
    Parameter bucketName: string name of the bucket
    Return: AWSTask
*/
func uploadImageToAWS (imageURL: NSURL, imageName: String, imageFile: UIImage, bucketName: String) -> AWSTask! {
    // create a file location for image
    UIImageJPEGRepresentation(imageFile, 1)!.writeToURL(imageURL, atomically: false)

    // save image
    let uploadRequest:AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest.bucket = bucketName
    uploadRequest.key = imageName
    uploadRequest.body = imageURL
    uploadRequest.uploadProgress = { (bytesSent:Int64, totalBytesSent:Int64,  totalBytesExpectedToSend:Int64) -> Void in
        dispatch_sync(dispatch_get_main_queue(), {() -> Void in
            print(totalBytesSent)
        })
    }

    return AWSS3TransferManager.defaultS3TransferManager().upload(uploadRequest)
}

在我的AppDelegate.swift中:

let credentialProvider = AWSCognitoCredentialsProvider(
        regionType: CognitoRegionType, identityPoolId: cognitoIdentityPoolId)        

let configuration = AWSServiceConfiguration(
        region: DefaultServiceRegionType,
        credentialsProvider: credentialProvider)

AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

然后在IAM的AWS控制台中,使用策略生成器,为S3存储桶创建角色策略(Unauth)。看起来像这样:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt123456789123",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::bucketname"
            ]
        }
    ]
}

最后,您需要在S3 Management Console中为存储桶权限添加存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "Policy1425419737188",
    "Statement": [
        {
            "Sid": "Stmt123456789123",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucketname/*"
        }
    ]
}

这可以通过以下方式生成:AWS Policy Generator