2015-05-29 33 views
3

IDE:XCode6 /斯威夫特iOS版雨燕:AWS SDK - 從S3下載文件 - 獲取內容,不保存文件

我試圖從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。
但我不想要保存文件,只需抓住內容 - 我該怎麼做?

回答

2

這是我用來下載圖片的Swift代碼。它不會保存圖像,它只是將其添加到我的viewDidLoad中聲明()我試圖用你的代碼,但得到「表達沒有downloadprogress成員」數組

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; 
    } 

} 
+0

。所以我猜這個成員不再退出或名稱改變了。 – user30646