使用alamofire从Amazon S3下载文件

时间:2018-12-31 08:03:32

标签: ios swift amazon-s3 download alamofire

我想从Amazon s3下载文件。这是我的代码

        let expression = AWSS3TransferUtilityDownloadExpression()

        expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in

            self.progressLabel.text = "Downloaded \(ByteCountFormatter.string(fromByteCount: progress.completedUnitCount, countStyle: .file)) of \(ByteCountFormatter.string(fromByteCount: progress.totalUnitCount, countStyle: .file) ) "
                self.progressUpload.setProgress(Float(progress.fractionCompleted), animated: true)

        }

        completionHandler = { (task, location, data, error) -> Void in
                if ((error) != nil){
                    print("Failed with error")
                    print("Error: \(error!)")
                }
                else{
                    //Set your image
                    var downloadedImage = UIImage(data: data!)
                }
        }

        let transferUtility = AWSS3TransferUtility.default()

        transferUtility.downloadToURL(nil, bucket: S3BucketName, key: saveFileWithName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
            if let error = task.error {
                print("Error: \(error.localizedDescription)")
            }
            if let exception = task.exception {
                print("Exception: \(exception.description)")
            }
            if let _ = task.result {
                print("Download Starting!")
            }
            return nil;
        }

但是给我一个错误,即“类型'AWSS3TransferUtilityDownloadExpression'的值没有成员'downloadProgress'”。

我可以使用alamofire下载吗?这是我使用alamofire下载简单文件的代码。但会下载简单的url文件,但不会下载amazon s3存储桶文件。请帮帮我。

        let url = URL(string: "\(AppConstants.ImageServerURL)\(fileUrl)")
        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL = Utilities.getDocumentsDirectory().appendingPathComponent("\(StringConstants.DownloadFolderName)/\(saveFileWithName).\(fileUrl.fileExtension())")
            print(documentsURL)
            return (documentsURL, [.removePreviousFile])
        }

        self.progressUpload.setProgress(0, animated: true)
        UIViewController.current().present(self.uploadAlertView, animated: true, completion: nil)
        self.request = Alamofire.download(
            url!,
            method: .get,
            parameters: nil,
            encoding: JSONEncoding.default,
            headers: ApplicationData.sharedInstance.authorizationHeaders,to: destination).downloadProgress(closure: { (progress) in
                //progress closure
                self.progressLabel.text = "Downloaded \(ByteCountFormatter.string(fromByteCount: progress.completedUnitCount, countStyle: .file)) of \(ByteCountFormatter.string(fromByteCount: progress.totalUnitCount, countStyle: .file) ) "
                self.progressUpload.setProgress(Float(progress.fractionCompleted), animated: true)
            }).response(completionHandler: { (response) in

                self.uploadAlertView.dismiss(animated: true, completion: nil)
                // check result is success
                guard response.error == nil else {

                    failure()
                    return
                }

                success()

            })

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案