iOS Swift:AWS SDK – 从S3下载文件 – 获取内容而不保存文件

iOS Swift:AWS SDK – 从S3下载文件 – 获取内容而不保存文件,第1张

概述IDE:XCode6 / Swift 我正在尝试从AWS S3下载文件,我已正确设置所有库,下载代码为(相关部分).. let downloadFilePath = "/Users/user1/myfile.json" //locally save file herelet downloadingFileURL = NSURL.fileURLWithPath(downloadFilePath) IDE:XCode6 / Swift

我正在尝试从AWS S3下载文件,我已正确设置所有库,下载代码为(相关部分)..

let downloadfilePath = "/Users/user1/myfile.Json" //locally save file herelet downloadingfileURL = NSURL.fileURLWithPath(downloadfilePath)...    let downloadRequest = awss3transfermanagerDownloadRequest()    downloadRequest.bucket = s3Bucketname    downloadRequest.key  = "myfile.Json" //filename on s3    downloadRequest.downloadingfileURL = downloadingfileURLlet transferManager = awss3transfermanager.defaultS3TransferManager()        transferManager.download(downloadRequest).continueWithBlock {            (task: BFTask!) -> AnyObject! in            if task.error != nil {                println("Error downloading")                println(task.error.description)            }            else {                println(downloadfilePath)                var mytext = String(contentsOffile: downloadfilePath,enCoding: NSUTF8StringEnCoding,error: nil)                println(mytext)            }

这很好 – 文件保存到/Users/user1/myfile.Json.
但我不希望文件被保存,只是抓住内容 – 我怎么能这样做?

解决方法 这是我用来下载图像的Swift代码.它不会保存图像,它只会将它添加到我在vIEwDIDLoad()中声明的数组中

func downloadImage(key: String){    var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?    //downloading image    let S3Bucketname: String = "your_s3_bucketname"    let S3DownloadKeyname: String = key    let Expression = AWSS3TransferUtilityDownloadExpression()    Expression.downloadProgress = {(task: AWSS3TransferUtilityTask,bytesSent: Int64,totalBytesSent: Int64,totalBytesExpectedToSend: Int64) in        dispatch_async(dispatch_get_main_queue(),{            let progress = float(totalBytesSent) / float(totalBytesExpectedToSend)            //self.progressVIEw.progress = progress            //   self.statusLabel.text = "Downloading..."            NSLog("Progress is: %f",progress)        })    }    completionHandler = { (task,location,data,error) -> VoID in        dispatch_async(dispatch_get_main_queue(),{            if ((error) != nil){                NSLog("Failed with error")                NSLog("Error: %@",error!);                //   self.statusLabel.text = "Failed"            }                /*                else if(self.progressVIEw.progress != 1.0) {                //    self.statusLabel.text = "Failed"                NSLog("Error: Failed - likely due to invalID region / filename")                }   */            else{                //    self.statusLabel.text = "Success"                self.collectionImages[S3DownloadKeyname] = UIImage(data: data!)                //reload the collectionVIEw data to include new picture                self.colVIEw.reloadData()            }        })    }    let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()    transferUtility.downloadToURL(nil,bucket: S3Bucketname,key: S3DownloadKeyname,Expression: Expression,completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in        if let error = task.error {            NSLog("Error: %@",error.localizedDescription);            //  self.statusLabel.text = "Failed"        }        if let exception = task.exception {            NSLog("Exception: %@",exception.description);            //  self.statusLabel.text = "Failed"        }        if let _ = task.result {            //    self.statusLabel.text = "Starting Download"            //NSLog("Download Starting!")            // Do something with uploadTask.            /*            dispatch_async(dispatch_get_main_queue(),{                self.colVIEw.reloadData()            })            */        }        return nil;    }}
总结

以上是内存溢出为你收集整理的iOS Swift:AWS SDK – 从S3下载文件 – 获取内容而不保存文件全部内容,希望文章能够帮你解决iOS Swift:AWS SDK – 从S3下载文件 – 获取内容而不保存文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1046400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存