How to upload UIDocumentPickerViewcontroller file into AWS S3 TransferUtility Swift

385 Views Asked by At

I am trying to pick some files from UIDocumentPickerView and upload to AWS S3 with TransferUtility. Here, I'm not able to pass the filename, size and file data to upload function. Also, upload function status need to show on UITableView.

Here below my code:

public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

    let fileurl: URL = url as URL
    let filename = url.lastPathComponent
    let fileextension = url.pathExtension
    print("URL: \(fileurl)", "NAME: \(filename)", "EXTENSION: \(fileextension)")

    myFiles.append(filename) //bad way of store
    util_TableView.reloadData()

}

Upload code:

func uploadImage(with data: Data) {
        let expression = AWSS3TransferUtilityUploadExpression()
        expression.progressBlock = progressBlock

        transferUtility.uploadData(
            data,
            bucket: S3BucketName,
            key: S3UploadKeyName,
            contentType: "image/png",
            expression: expression,
            completionHandler: completionHandler).continueWith { (task) -> AnyObject! in
                if let error = task.error {
                    print("Error: \(error.localizedDescription)")

                    DispatchQueue.main.async {
                        self.statusLabel.text = "Failed"
                    }
                }

                if let _ = task.result {

                    DispatchQueue.main.async {
                        self.statusLabel.text = "Generating Upload File"
                        print("Upload Starting!")
                    }

                    // Do something with uploadTask.
                }

                return nil;
        }
    }
1

There are 1 best solutions below

0
On

Use the API AWSS3TransferUtility.uploadFile. It can use the file localuri as the parameter so that you don't have to pass the file Data to the function, to upload the file.

Here is an answer to this issue. Swift upload multiple files parallel into AWS S3 and show progress view status in tableview cell