2

Attempting to upload an image to s3, but it fails each time for unknown reasons.

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
}

Upload starts but fails after a short period of time. The log:

Progress 0.126221

Progress 0.252442

Progress 0.378663

Progress 0.504884

AWSiOSSDKv2 [Error] AWSURLSessionManager.m line:260 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:] | Session task failed with error: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSUnderlyingError=0x7fda2f90e3c0 {Error Domain=kCFErrorDomainCFNetwork Code=-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=cannot parse response}

Transfer manager initialization:

 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")

Can I somehow get more information on why the upload failed?

Mercurial
  • 2,095
  • 4
  • 19
  • 33

1 Answers1

0

Code -1017 means that the server is not responding with a valid HTTP response. Switch on verbose logging to get more information:

    AWSLogger.defaultLogger().logLevel = .Verbose

I use the following to upload to 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)
}

And in my AppDelegate.swift:

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

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

AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

Then in the AWS Console in IAM, using the policy generator, I create a Role Policy (Unauth) for the S3 bucket. Which will look like this:

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

Lastly you need to add a bucket policy to the bucket permissions in the S3 Management Console:

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

This can be generated with: AWS Policy Generator

Ruud Kalis
  • 264
  • 3
  • 9