lundi 4 mai 2015

Uploading Image to S3 stalls then restarts

I'm uploading an image to S3 and am having trouble completing the file transfer. Here is how the app acts.

  1. Initiate uploadToS3()
  2. File begins transfer sending bytes to server.
  3. When around 600,000 bytes are sent, the upload halts.
  4. After 20-40 seconds, the app continues its upload progress at 0%. It acts as if the file transfer never began in the first place

In my view controller I have the following method that uploads the file.

func uploadToS3(){

    // get the image from a UIImageView that is displaying the selected Image
    var img: UIImage = imageView.image!

    // create a local image that we can use to upload to s3
    var path: NSString = NSTemporaryDirectory().stringByAppendingPathComponent("image.png")
    var imageData: NSData = UIImagePNGRepresentation(img)
    imageData.writeToFile(path as String, atomically: true)

    // once the image is saved we can use the path to create a local fileurl
    var url:NSURL = NSURL(fileURLWithPath: path as String)!

    // next we set up the S3 upload request manager
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    // set the bucket
    uploadRequest?.bucket = "test-bucket"
    // I want this image to be public to anyone to view it so I'm setting it to Public Read
    uploadRequest?.ACL = AWSS3ObjectCannedACL.PublicRead
    // set the image's name that will be used on the s3 server. I am also creating a folder to place the image in
    uploadRequest?.key = "foldername/image.png"
    // set the content type
    uploadRequest?.contentType = "image/png"
    // and finally set the body to the local file path
    uploadRequest?.body = url;

    // we will track progress through an AWSNetworkingUploadProgressBlock
    uploadRequest?.uploadProgress = {[unowned self](bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in

        dispatch_sync(dispatch_get_main_queue(), { () -> Void in
            println("total  bytes sent")
            println(totalBytesSent)

            println("total  bytes expected to send")
            println(totalBytesExpectedToSend)
        })
    }

    // now the upload request is set up we can creat the transfermanger, the credentials are already set up in the app delegate
    var transferManager:AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager()
    // start the upload
    transferManager.upload(uploadRequest).continueWithExecutor(BFExecutor.mainThreadExecutor(), withBlock:{ [unowned self]
        task -> AnyObject in

        // once the uploadmanager finishes check if there were any errors
        if(task.error != nil){
            println("%@", task.error);
        }else{ // if there aren't any then the image is uploaded!
            // this is the url of the image we just uploaded
            println("http://ift.tt/1EbLvtA");
        }

        //self.removeLoadingView()
        println("all done");
        return ""
        })
}

For anyone looking to recreate this app

Add to your Podfile:

pod 'AWSCore'
pod 'AWSS3'
pod 'AWSiOSSDKv2'
pod 'AWSCognitoSync'

Then add a bridge header containing:

#import <AWSCore/AWSCore.h>
#import <AWSS3/AWSS3.h>

In my AppDelegate I have:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
    // Override point for customization after application launch.
    AWSCognitoCredentialsProvider.initialize()

    var credentialsProvider = AWSCognitoCredentialsProvider(
        regionType: AWSRegionType.USEast1,
        identityPoolId: "identity pool id"
    )

    var configuration = AWSServiceConfiguration(
        region: AWSRegionType.USEast1,
        credentialsProvider: credentialsProvider
    )

    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

    return true
}

Finally, in the view controller that contains uploadToS3(), add import AWSS3.

My question is, how do I fix this and have the image upload succesfully.




Aucun commentaire:

Enregistrer un commentaire