IOS Swift: AWS SDK - downloading a file from S3 - retrieving content without saving the file

IDE: Xcode6 / Swift

I am trying to download a file from AWS S3, I have correctly configured all the libraries, the download code (the corresponding part).

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

This works fine - the file is saved in / Users / user 1 / myfile.json.
But I do not want the file to be saved, just grab the contents - how can I do this?

+4
source share
1 answer

This is the Swift code that is used to upload images. It will not save the image, it will just add it to the array that I declared in 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;
    }

}
+2
source

All Articles