2

This is likely a very easy answer. I am fairly new to Swift and just getting feet wet with AWS. I simply want to download a JSON file to a string var in Swift so that I can parse, etc. I'm not able to find a good example (other than a repetitive one showing how to download an image).

I've seen the image example where the completion handler accesses the data i.e. UIImage(data: data!)! but I can't find the equivalent for a text file.

I know that I am getting in via credentials and configuration and that it is all correct. I just can't coax the text data out of the result. Here is the snippet I think should return what I want. I just need to know where it is...

func getJSON() {

    let S3BucketName: String = "mybucket"
    let S3DownloadKeyName: String = "myfile.txt"

    let expression = AWSS3TransferUtilityDownloadExpression()
    expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
        print("Downloading...")
    })
    }

    let transferUtility = AWSS3TransferUtility.default()
    transferUtility.downloadData(fromBucket: S3BucketName, key: S3DownloadKeyName, expression: expression) { (task, URL, data, error) in
            if error != nil {
                print(error!)
                return
            }
            DispatchQueue.main.async(execute: {
                print("Got here")
                // I would expect to find data in an attribute here
            })
    }
}

I would think the actual string could be coaxed from the "data" value in the completion handler, but I simply am not sure how to reach it or what property it exists in. I'm pretty sure I did an exhaustive search of a similar example that I could understand, but didn't find anything.

Any help is greatly appreciated.

Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
S Graham
  • 232
  • 2
  • 14
  • Wow, that was fast. Thanks @Larme - works exactly as I expected. So, I had tried String(data: data) getting the error: "Argument labels '(data:)' do not match any available overloads". Is there a brief explanation of why the encoding to UTF-8 is required)? Many thanks... – S Graham Mar 28 '19 at 14:48
  • Well you need to specify which String encoding method was using to create the data. Usually it is utf8 but it could be any (utf8, ascii, isoLatin1, isoLatin2, ...) https://developer.apple.com/documentation/swift/string/encoding – Leo Dabus Mar 28 '19 at 16:15
  • Yes, it makes perfect sense to me now that it's working and I have had a little more time to understand. Thanks. – S Graham Mar 28 '19 at 16:21
  • Btw better to keep it as data, create a custom structure that conforms to Decodable and use JSONDecoder to decode your json data. – Leo Dabus Mar 28 '19 at 16:23
  • Thanks, I can certainly see that as a cleaner solution. – S Graham Mar 29 '19 at 02:06

1 Answers1

3

Adding this answer because @Larme's comment disappeared. Very simply the data returned needed to be encoded. This worked:

    let transferUtility = AWSS3TransferUtility.default()
    transferUtility.downloadData(fromBucket: S3BucketName, key: S3DownloadKeyName, expression: expression) { (task, URL, data, error) in
            if error != nil {
                print(error!)
            }
            DispatchQueue.main.async(execute: {
                print("Got here")
                self.rawJSON = (String(data: data!, encoding: .utf8))!
                print(self.rawJSON)
            })
    }
S Graham
  • 232
  • 2
  • 14