'AWS Transfer Utility uploads are slow and inconsistent in iOS

I have implemented the basic AWS transfer utility upload video (file) code in my app and this had been working for me flawlessly until recently the uploads got extremely slow and even stuck. I tried changing many things in the AWS code like shifting from TransferUtilityUpload to TrasferUtility UploadUsing MultiPart, changing the AWSServiceConfiguration from AWSCognitoCredentialsProvider(using poolId & region) to AWSStaticCredentialsProvider (using Accesskey, secret key and region), enabling acceleration etc but nothing has helped to increase the upload speed. Apart from this, the uploads are very inconsistent. For example sometimes a 30sec video (size 180MB) gets uploaded in under 2 mins and then again same video takes more than 5 minutes/gets stuck in the same network (speed 150MBps or more)

Can someone please help me understand the issue and fix it?

Code snippets below.

Service Configuration

let credentialsProvider = AWSStaticCredentialsProvider(accessKey: "******", secretKey: "*****")
let configuration = AWSServiceConfiguration.init(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

AWS Upload function

private func uploadfile(fileSize: Int, fileUrl: URL, fileName: String, contenType: String, progress: progressBlock?, completion: completionBlock?) {
        // Upload progress block
        var previousUploadedBytes: Double = 0.0
        let expression = AWSS3TransferUtilityMultiPartUploadExpression()
        expression.progressBlock = {(task, awsProgress) in
            if task.status == AWSS3TransferUtilityTransferStatusType.waiting {
                task.cancel()
            }
            guard let uploadProgress = progress else { return }
            DispatchQueue.main.async {
                uploadProgress(awsProgress.fractionCompleted)
                
                //CODE FOR UI UPDATES
               //DO SOMETHING WITH THE PROGRESS
            }
        }

        // Completion block
        var completionHandler: AWSS3TransferUtilityMultiPartUploadCompletionHandlerBlock?
        completionHandler = { (task, error) -> Void in
            DispatchQueue.main.async(execute: {
                if error == nil {
                    let url = AWSS3.default().configuration.endpoint.url
                    let publicURL : URL = (url?.appendingPathComponent(self.bucketName).appendingPathComponent(fileName))!

                    if let completionBlock = completion {
                        completionBlock(publicURL.absoluteString, nil)
                    }
                } else {
                    if let completionBlock = completion {
                        completionBlock(nil, error)
                    }
                }
            })
        }
        
        //acceleration mode enabled
        let serviceConfiguration = AWSServiceConfiguration(
            region: .USEast1,
            credentialsProvider: AWSServiceManager.default().defaultServiceConfiguration.credentialsProvider
        )
        
        let transferUtilityConfiguration = AWSS3TransferUtilityConfiguration()
        transferUtilityConfiguration.isAccelerateModeEnabled = true
        
        AWSS3TransferUtility.register(
            with: serviceConfiguration!,
            transferUtilityConfiguration: transferUtilityConfiguration,
            forKey: "transfer-acceleration"
        )

        
        // Start uploading using AWSS3TransferUtility
        let awsTransferUtility = AWSS3TransferUtility.default()
        awsTransferUtility.uploadUsingMultiPart(fileURL: fileUrl, bucket: bucketName, key: fileName, contentType: contenType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> Any? in
            if let error = task.error {
                UploadHelper.sharedInstance.showSSLError = false
                if (error as NSError).code == -1001 {
                    DispatchQueue.main.async {
                        UploadHelper.sharedInstance.noOfRetries = 0
                        UploadHelper.sharedInstance.changeToRetryUpload() // internal code to call for retry
                    }
                } else if (error as NSError).code == -1009 {
                    DispatchQueue.main.async {
                        UploadHelper.sharedInstance.noOfRetries = 0
                        UploadHelper.sharedInstance.changeToRetryUpload() // internal code to call for retry
                    }
                } else if (error as NSError).code == -1003 {
                    DispatchQueue.main.async {
                        UploadHelper.sharedInstance.noOfRetries = 0
                        UploadHelper.sharedInstance.changeToRetryUpload()  // internal code to call for retry
                    }
                } else if (error as NSError).code == -1200 {
                    DispatchQueue.main.async {
                        UploadHelper.sharedInstance.noOfRetries = 0
                        UploadHelper.sharedInstance.changeToRetryUpload() // internal code to call for retry
                        UploadHelper.sharedInstance.showSSLError = true
                    }
                }
            }
            if let _ = task.result {
                // your uploadTask
            }
            return nil
        }
        
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source