AWS ios SDK - uploading objects to AWS S3

4k views Asked by At

I'm trying to follow the steps listed here under "Upload an Object": http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transfermanager.html

However, I'm converting things over to Swift because that's what my whole application is written in. I've come up with the following code, but when I run it nothing gets uploaded to the bucket I specified. Unfortunately I don't even get an error, which makes it hard to troubleshoot - you can see below that I'm trying to print any error to the console, but myBFTask.error ends up being null. At the same time myBFTask.result is also null. For context, this code is all inside the imagePickerController didFinishPickingMediaWithInfo. Any tips on where to look next would be greatly appreciated.

        var pickedURL:NSURL = info[UIImagePickerControllerMediaURL] as NSURL
        println("here's the url for the picked media: \(pickedURL)")

        //make a timestamp variable to use in the key of the video I'm about to upload
        let date:NSDate = NSDate()
        var unixTimeStamp:NSTimeInterval = date.timeIntervalSince1970
        var unixTimeStampString:String = String(format:"%f", unixTimeStamp)
        println("this is my unix timestamp as a string: \(unixTimeStampString)")

        var myTransferManagerRequest:AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
        myTransferManagerRequest.bucket = "kikfli1.videolist"
        myTransferManagerRequest.key = "\(self.fbid)_\(unixTimeStampString)"
        myTransferManagerRequest.body = pickedURL

        var myBFTask:BFTask = BFTask()
        var myMainThreadBFExecutor:BFExecutor = BFExecutor.mainThreadExecutor()
        var myTransferManager:AWSS3TransferManager = AWSS3TransferManager()
        myTransferManager.upload(myTransferManagerRequest).continueWithExecutor(myMainThreadBFExecutor, withBlock: { (myBFTask) -> AnyObject! in
            println("I'm inside the completion block")
            if((myBFTask.result) != nil){
                println("upload was successful?")
            }else{
                println("upload didn't seem to go through..")
                var myError = myBFTask.error
                println("error: \(myError)")
            }
            return nil
        })

Here is what gets printed to my console:

the user picked a video for submission!

here's the url for the picked media: file:xxxxxxxxxxxxxxx.MOV (obviously an actual file path)

this is my unix timestamp as a string: 1417506382.414219

I'm inside the completion block

upload didn't seem to go through..

error nil

2

There are 2 answers

2
Yosuke On BEST ANSWER

You are not passing AWSServiceConfiguration to AWSS3TransferManager, and it's causing the issue. You need to change the following line:

var myTransferManager:AWSS3TransferManager = AWSS3TransferManager()

to something like

var myTransferManager:AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager()

Also, you need to set the default service configuration to the default service manager in advance.

0
Andrej On

I had a similar problem. The app crashed when trying to upload with fatal error: unexpectedly found nil while unwrapping an Optional value.

After double-checking that file exists at path I figured out that AWSS3TransferManager was not getting constructed, because I had forgot to set the default configuration (3rd line):

let credentialsProvider = AWSStaticCredentialsProvider(accessKey: AccesKey, secretKey: SecretKey)
let configuration = AWSServiceConfiguration(region: ServiceRegionType, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration