Amazon-S3 [SOLVED]: Unable to download images from AWS S3, Swift 3

Amazon-S3 [SOLVED]: Unable to download images from AWS S3, Swift 3

Home Forums Amazon Web Services Amazon S3 Amazon-S3 [SOLVED]: Unable to download images from AWS S3, Swift 3

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #247009

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have images uploaded to AWSS3 which I am trying to download using the below code.

    The code does not run through transferManager.download(downloadRequest).continueWith(executor: AWSExecutor.default(), block: { (task: AWSTask<AnyObject>) -> Any? in, can someone please advice how I could fix this.

    AWS SDK versions used (from pod file)
    pod 'AWSCore', '~> 2.6.7'
    pod 'AWSS3', '~> 2.6.7'

    static func testDownload(){ 
    
        let transferManager = AWSS3TransferManager.default()
    
        let downloadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("temp.jpg")
    
        if let downloadRequest = AWSS3TransferManagerDownloadRequest(){
    
            downloadRequest.bucket = "bucket-name"
            downloadRequest.key = "folderName/imageName.png"
            downloadRequest.downloadingFileURL = downloadingFileURL
            print("downloadRequest (downloadRequest)")
            transferManager.download(downloadRequest).continueWith(executor: AWSExecutor.default(), block: { (task: AWSTask<AnyObject>) -> Any? in
    
                print("transferManager.download func called")
    
                if( task.error != nil){
                    print(task.error!.localizedDescription)
                    return nil
                }
    
                print(task.result!)
    
                if let data = NSData(contentsOf: downloadingFileURL){
    
                    DispatchQueue.main.async(execute: { () -> Void in
                        let image = UIImage(data: data as Data)
                        print("Downloaded Image: (image)")
                    })
                }
                return nil
            })
        }
    }
    

    #247010

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    That block is a completion handler which would be called after the download has been completed. You can use the following code to track the progress of download as it happens:

    downloadRequest.downloadProgress = {(bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) -> Void in
        DispatchQueue.main.async(execute: {() -> Void in
            //Update progress
        })
    }
    

    Also, you can try using the main thread for testing purpose to see if it’s something wrong with your bucket configuration or info.plist values or something else..

    transferManager.download(downloadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
    
        if let error = task.error as? NSError {
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                switch code {
                case .cancelled, .paused:
                    break
                default:
                    print("Error downloading: (downloadRequest.key) Error: (error)")
                }
            } else {
                print("Error downloading: (downloadRequest.key) Error: (error)")
            }
            return nil
        }
        print("Download complete for: (downloadRequest.key)")
        let downloadOutput = task.result
        return nil
    })
    

    Source: http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transfermanager.html

    Source: https://stackoverflow.com/questions/47974509/unable-to-download-images-from-aws-s3-swift-3
    Author: Anushk
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.