iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS Swift:AWS SDK – 从S3下载文件 – 获取内容而不保存文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
IDE:XCode6 / Swift

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

let downloadFilePath = "/Users/user1/myfile.json" //locally save file here
let downloadingFileURL = NSURl.fileURLWithPath(downloadFilePath)
...

    let download@R_944_10613@est = AWSS3TransfeRMANagerDownload@R_944_10613@est()
    download@R_944_10613@est.bucket = s3BucketName
    download@R_944_10613@est.key  = "myfile.json" //filename on s3
    download@R_944_10613@est.downloadingFileURL = downloadingFileURL

let transfeRMANager = AWSS3TransfeRMANager.defaultS3TransfeRMANager()
        transfeRMANager.download(download@R_944_10613@est).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,@R_541_10586@lBytesSent: Int64,@R_541_10586@lBytesExpectedToSend: Int64) in
        dispatch_async(dispatch_get_main_queue(),{
            let progress = Float(@R_541_10586@lBytesSent) / Float(@R_541_10586@lBytesExpectedToSend)
            //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下载文件 – 获取内容而不保存文件所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。