如何将UIDocumentPickerViewcontroller文件上传到AWS S3 TransferUtility Swift

时间:2018-08-13 12:36:18

标签: ios swift amazon-web-services awss3transferutility

我正在尝试从UIDocumentPickerView中选择一些文件,然后使用TransferUtility上传到AWS S3。在这里,我无法将文件名,大小和文件数据传递给上传功能。此外,上传功能状态需要显示在UITableView上。

下面是我的代码:

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()

}

上传代码:

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 个答案:

答案 0 :(得分:0)

使用API​​ AWSS3TransferUtility.uploadFile。它可以使用文件localuri作为参数,这样您就不必将文件数据传递到函数中来上传文件。

这是此问题的答案。 Swift upload multiple files parallel into AWS S3 and show progress view status in tableview cell