iOS swift : AWS SDK - downloading file from S3 - get contents without saving file

标签 ios swift amazon-s3

集成开发环境:XCode6/Swift

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

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

    let downloadRequest = AWSS3TransferManagerDownloadRequest()
    downloadRequest.bucket = s3BucketName
    downloadRequest.key  = "myfile.json" //fileName on s3
    downloadRequest.downloadingFileURL = downloadingFileURL

let 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 - downloading file from S3 - get contents without saving file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30525157/

相关文章:

ios - TLIndexPathTools - 动态加载树 TableView ?

swift - 以编程方式在 SKScene 和 View Controller 之间切换?

java - AWS SDK - AmazonS3ClientBuilder 实例是否过期?

java - 如何在 jasper 报告中显示亚马逊 S3 图像

python - Apache Spark 读取 S3 : can't pickle thread. 锁对象

ios - 使用 AVFoundation,我可以在用户按下按钮时捕获最后 5 秒吗?

ios - 如何更改 UINavigationController 推送过渡样式?

ios - 如何让两个 NSManagedObject 类具有相同的父类(super class)

ios - Apple Swift 的类型转换义务

ios - Swift iOS,想知道是否有人可以解释为什么输出旋转到正确的度数?