Accessing video file path URL on an IOS device in SWIFT

493 Views Asked by At

I am currently trying to upload a Video file to AWSS3 using the AWS IOS SDK. Here is my process:

// I accept a video Data from UIIMagepicker func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    var videoPath = "VideoPath"
    if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] {
        print("Video URL: \(videoURL)")
        
        
        
    
        uploadVideo(resource: "RandomName", type: "mov", url: videoURL as! URL)
        thumbnailImageView.image = 

}

//I then call this upload video function that makes the AWSS#transferutilityuploadtask of the video.

let bucketName = “somebucketName"
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
func uploadVideo(resource: String, type: String , url: URL){   //1
    
    
        
        let key = "Tester.mov"
        //let resource = Bundle.main.path(forResource: resource, ofType: type)!
        let url = url
       
    
    print(url)
    
    
    
  
    
    
    
       // let url = url
        
        let expression  = AWSS3TransferUtilityUploadExpression()
    
        expression.progressBlock = { (task: AWSS3TransferUtilityTask,progress: Progress) -> Void in
            print("Progress: \(progress.fractionCompleted * 100 ) %")//2
            print("Task state: \(task.progress)")
        
            
          if progress.isFinished{           //3
            print("100%...Upload Finished...")
            print("finished Uploading \(url)")
            
            DispatchQueue.main.async {
                var alert = UIAlertController(title: "Congratulations!",
                            message: "Successfully Uploaded Video",
                            preferredStyle: .alert)
                        self.present(alert, animated: true, completion:nil)
                
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {
                    (action) in
                    
                    
                }))
            }
      
            //do any task here.
          }
        }
        
        expression.setValue("public-read-write", forRequestHeader: "x-amz-acl")   //4
        expression.setValue("public-read-write", forRequestParameter: "x-amz-acl")

        completionHandler = { (task:AWSS3TransferUtilityUploadTask, error:NSError?) -> Void in
            if(error != nil){
                print("Failure uploading file")
                
            }else{
            
                print("Success uploading file!\(print(task.response))")
                
               
            }
        } as? AWSS3TransferUtilityUploadCompletionHandlerBlock
        
        

        //5
    AWSS3TransferUtility.default().uploadFile(url, bucket: bucketName, key: String(key), contentType: "video", expression: expression, completionHandler: self.completionHandler).continueWith(block: { (task:AWSTask) -> AnyObject? in
            if(task.error != nil){
                
                print("Error uploading file: \(String(describing: task.error?.localizedDescription))")
                
            }
            if(task.result != nil){
                print("Starting upload...")
                
                
            }
            return nil
        })
    }

I have also initialized the AWSS3 transfer connection in my apps delegate.

This code works perfect in my simulator. It takes a file from a local file path url and uploads it to my AWS Bucket. However when i run it on a device the local path doesnt seem to get access to the video data. The program stops at “Starting upload.." and shows 0 as the progress

I noticed that the urls created from the device and the sumulator are different. Not sure how to accept both but for some reason the device url is not providing proper access.

Simulator VideoURL file:///Users/dominiq/Library/Developer/CoreSimulator/Devices/66E08504-7B2B-4DC3-A473-F35E7BB575F6/data/Containers/Data/PluginKitPlugin/9E664198-094E-458F-A1C0-C0E0E3F9A454/tmp/trim.2F6719E1-B2AC-4DF0-BA59-319A85B770DF.MOV

IOS Device VideoURL:

file:///private/var/mobile/Containers/Data/PluginKitPlugin/B62449E7-5EE3-4EAB-A5AC-F7C867928959/tmp/trim.C18D77B0-6247-40BB-A175-A1B058627F9D.MOV

0

There are 0 best solutions below