Swift UIDocumentPickerViewController文档上传到AWS S3

时间:2018-08-13 18:31:15

标签: ios swift amazon-web-services tableview

UIDocumentPickerViewController可以选择一些文档,并在TransferUtility的帮助下上传到AWS S3。在这里,上传时,我需要将上传文件的名称,状态(渐进加载程序%),大小显示到tableview单元格中。如果我上传多个文件,则tableview单元需要显示具有加载状态的多个单元。

问题:

  1. 我已经完成UIDocumentPickerViewController来选择一些文档并获取那些文档的URL,名称,大小等。但我不知道如何使用(或)将这些值传递到上载部分。

    // MARK - File Storage Access
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    
        let fileurl: URL = url as URL
        let filename = url.lastPathComponent
        let fileextension = url.pathExtension
        let filedata = url.dataRepresentation
        print("DATA: \(filedata)","URL: \(fileurl)", "NAME: \(filename)", "EXTENSION: \(fileextension)")}
    

    //需要存储以上值并将其用于以下功能

  2. 在上传部分下方,如何与表格视图单元进行交互。


transferUtility.uploadData(data,bucket: S3BucketName,key: name,contentType: "text/plain",expression: expression,completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
    if let error = task.error {
        print("Error: \(error.localizedDescription)")
        DispatchQueue.main.async {
            cells.statusLabel_util.text = "Failed"
            print("Failed")
        }
    }

    if let _ = task.result {

        DispatchQueue.main.async {
            print("Upload Starting!")
        }
        // Do something with uploadTask.
    }
    return nil;
}

1 个答案:

答案 0 :(得分:0)

您可以使用此代码上传文件并跟踪进度

if let uploadRequest = AWSS3TransferManagerUploadRequest(){

            uploadRequest.bucket = "your bucket"
            uploadRequest.key = fileName
            //uploadRequest.acl = AWSS3ObjectCannedACL.publicRead
            uploadRequest.body = fileUrl

            uploadRequest.uploadProgress = {(bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) -> Void in
                DispatchQueue.main.async(execute: {() -> Void in
                    //Update progress
                })
            }

if let transferManager = AWSS3TransferManager.default(){

            transferManager.upload(uploadRequest).continue(with: AWSExecutor.mainThread(), with: { (task) -> Any? in

                if let error = task.error as NSError? {

                    if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                        switch code {
                        case .cancelled, .paused:

                            break
                        default:

                            break
                            //print("Error uploading: \(uploadRequest?.key) Error: \(error)")
                        }
                    } else {
                        //print("Error uploading: \(uploadRequest?.key) Error: \(error)")
                    }
                    return nil
                }

                return nil
            })
          }

        }