user1408682
user1408682

Reputation: 429

Swift/Objective S3 File upload

I am trying to use the new version of AWS that was recently released, I have the older version working on android but i am new to swift and the newer version of AWS S3 and have been blocked for the last couple of days with this issue. I keep on getting a error message of The operation couldn’t be completed. (Cocoa error 260.)

Here is my app delegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let credentialsProvider = AWSCognitoCredentialsProvider.credentialsWithRegionType(
        AWSRegionType.USEast1,
        accountId: cognitoAccountId,
        identityPoolId: cognitoIdentityPoolId,
        unauthRoleArn: cognitoUnauthRoleArn,
        authRoleArn: cognitoAuthRoleArn)
    let defaultServiceConfiguration = AWSServiceConfiguration(
        region: AWSRegionType.USEast1,
        credentialsProvider: credentialsProvider)
    AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(defaultServiceConfiguration)
  AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(defaultServiceConfiguration)
    return true
}

Code of where i am attempting to upload a image to the bucket

    let fileUrl = NSURL(string:imagePath!)
    println(imagePath)
    UIImageJPEGRepresentation(self.image, 1).writeToURL(fileUrl, atomically: true)
    var indent:NSString = "closr"
    var uploadRequest:AWSS3TransferManagerUploadRequest =        AWSS3TransferManagerUploadRequest()
    uploadRequest.bucket = "closr-bucket"
    uploadRequest.key = "filename.jpg"
    uploadRequest.contentType = "image/jpeg"
    uploadRequest.body = fileUrl
    uploadRequest.uploadProgress = { (bytesSent:Int64, totalBytesSent:Int64,  totalBytesExpectedToSend:Int64) -> Void in
        dispatch_sync(dispatch_get_main_queue(), {() -> Void in
            println(totalBytesSent)
        })
    }

          AWSS3TransferManager.defaultS3TransferManager().upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
        if (task.error != nil) {
                //failed
                println("failed")
                println(task.error.code)
                println(task.error.localizedDescription)
        } else {
            //completed
            println("completed")
        }
        return nil
    }

Upvotes: 2

Views: 1936

Answers (1)

try using fileURLWithPath class method to instantiate the NSURL object, this is the right way to access local files using NSURL.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/clm/NSURL/fileURLWithPath:isDirectory:

Upvotes: 3

Related Questions