Sunday, 12 August, 2018 UTC


Summary

Using AWS for file storage is a great way to add data upload capabilities to your mobile apps. It can easily be added to your app and give you inexpensive storage space online.
If you have AWS Authentication added to your app it can allow users to upload files specifically for their account. Otherwise, you can allow users access to files across the board. This is great for file sharing, social content and more.
We’ll look at the AWS side of things and then the iOS project needs.
AWS
The first thing you need to do is create an AWS account here: aws.amazon.com. You’ll want to check out the pricing (https://aws.amazon.com/pricing/) but the Free Tier should be fine for getting started.
From there we’ll go to the Mobile Hub and create our project: console.aws.amazon.com/mobilehub/home
The first time you load the Mobile Hub you’ll be prompted to create a project:
Later, after you’ve created a project, this screen will list your projects.
Step 1: Select iOS
For demonstration purposes, we’ll select iOS as our platform.
Step 2: Cloud enable your existing app > Start
We won’t be using their tutorials so under Step 2 select the Start button in the box for “Cloud enable your existing app.” This will allow us to create our own app in Xcode.
Name and Next
All we have to do next is give our project a name: MyAWSApp (image below). And click Next.
The next screen (no image) has a prepared configuration file for you to download. However, we’ll hold off on this for now. We’re just going to have to download it again once we’ve configured the file storage anyway.
Click Next. On the following screen (image below), there’s a link to add the mobile SDK (which we’ll do later) so just click Done.
Now we see our project’s listing of apps (one, iOS) and the Backend features:
AWS automatically configured Messaging and Analytics for us. That’s in the configuration file already (the one we didn’t download yet). If you scroll down you can see other backend features you can add to your project. We’ll see User File Storage (not in the image above).
Scroll down and click User File Storage.
On the next screen you’ll see the very simple process of enabling the storage of files.
Simply click on Store Files and save your changes. Easy, right?
Notice the banner at the top informing you that the back end has been updated and you need to integrate:
Now well click on the blue banner:
And then click on the pulsating Integrate button
Now we’ll click on Download Cloud Config button (no image). This will download the awsconfiguration.json file. This is what we’ll use to configure our backend into our iOS app in Xcode.
Let’s look at the config file… (image below)
Notice the settings for S3TransferUtility. This specifies a bucket that was created for us in S3. You can see the bucket created for you by going to https://console.aws.amazon.com/s3/home 
The bucket name will contain the name of the app, mobile hub and the same number at the end. For example: myawsapp-userfiles-mobilehub-23423523532.
iOS Xcode Project
Now we’ll switch over to Xcode for our iOS project. For Android details of the same process, see https://docs.aws.amazon.com/aws-mobile/latest/developerguide/getting-started.html#add-aws-mobile-sdk-connect-to-your-backend 
First we need to create our project in Xcode:
  1. Open Xcode
  2. Select File>New Project>iOS Single View Template
  3. Name your project and select where to store it (image).
Now we close Xcode to install the CocoaPods.
Open a Terminal and navigate to the directory where you stored your project.
If you haven’t installed CocoaPods before, run “sudo gem install cocoapods” – see https://cocoapods.org for details.
The run “pod init” to create your Podfile.
Edit that Podfile and add the lines for the pods we need:
pod ‘AWSMobileClient’, ‘~> 2.6.13’
pod ‘AWSS3’, ‘~> 2.6.13’  
Your pod fille will look like this:
Then run “pod install” to install the pods and create your Xcode workspace.
Once installed the .xcworkspace is what you want to use going forward – not the .xcproject.
Run “open <projectname>.xcworkspace” to open it.
Now you have a workspace that includes your app and the CocoaPods needed.
Drag your awsconfiguration.json file into your project navigator:
Open the AppDelegate.swift file, import AWSMobileClient (line 10 below), the AWS mobile client framework, and add the client shared instance return to the first function (lines 20-22 below).
NOTE: You might see an error until you build the project which now includes the pods.
Your didFinishLaunchingWithOptions function will now also start the mobile client.
File Upload
Now we’ll upload the file…
Open the ViewController.swift file and import AWS3 – the S3 CocoaPod:
import AWSS3
Write a new function inside your ViewController class to upload data (below). We’ll simply create a data object from a String and upload that.
Be sure to call this function from your code (e.g., from viewDidLoad).
When you run your app (Command+R), you’ll see that “no error” is printed to the console. You can then check the S3 bucket (remember the name from the awsconfiguration.json file) and see the file.
If you click on the file you can open it or download it and confirm the contents.
Downloading is similar and can be done to data or file (represented in the URL sent to the callback). The calls are downloadData and download respectively.
func downloadFile() {

    AWSS3TransferUtility.default().downloadData(

            forKey: "public/hello.txt",

            expression: nil) { (task, url, data, error) in

        print (String.init(data: data!, encoding: .utf8)!)

    }

}
Of course a lot more can be done including progress of uploads and downloads. I’d recommend you see more at aws.console.com and various courses online.
I encourage you to navigate around in the AWS Mobile Hub, Cognito and AWS IAM:
https://console.aws.amazon.com/mobilehub/ 
https://console.aws.amazon.com/cognito/ 
https://console.aws.amazon.com/iam/ 
Also see my online courses with related topics here:
LiL: https://www.linkedin.com/learning/instructors/bear-cahill?trk=insiders_676521_learning 
Lynda.com: https://www.lynda.com/Bear-Cahill/10916794-1.html 
And feel free to visit me online at brainwashinc.com and @brainofbear
The post AWS Mobile Hub File Storage for Mobile Apps appeared first on appendTo.