Amazon Simple Storage Service (S3) provides secure, durable, highly scalable object storage in the cloud. Using the AWS Mobile SDK for iOS, you can directly access Amazon S3 from your mobile app.
With AWS object storage solutions like Amazon Simple Storage Service (Amazon S3) and Amazon Glacier, you manage your storage in one place with an easy-to-use application interface. You can use policies to optimize storage costs, tiering between different storage classes automatically. AWS makes storage easier to use to perform analysis, gain insights, and make better decisions faster.
Amazon S3 Benefits
Amazon S3 provides the most powerful object storage platform available in the cloud today.
Unmatched durability, reliability, & scalability.
Most comprehensive security & compliance capabilities.
Query in place.
4 Flexible management.
Most supported platform with the largest ecosystem.
Easy, flexible data transfer.
Getting Started with AWS S3
Amazon Simple Storage Service (Amazon S3) is storage for the Internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web. You can accomplish these tasks using the AWS Management Console, which is a simple and intuitive web interface. You can get the whole documentation from here – AWS s3
Before proceeding further please follow the documentation to create the s3 account and as well the bucket. Every object in Amazon S3 is stored in a bucket. Before you can store data in Amazon S3, you must create a bucket.
Let’s get started with integrating AWS SDK to iOS app and build a sample to:
The AWS Mobile Android and iOS SDKs help you build high quality mobile apps quickly and easily. They provide easy access to a range of AWS services, including Amazon Cognito, AWS Lambda, Amazon S3, Amazon Kinesis, Amazon DynamoDB, Amazon Pinpoint and many more.
Please check set-up here how to configure the iOS app and do necessary configurations. We need to configure “Add User DataStorage” to the iOS app we will be creating in the AWS mobile hub.
User Data Storage
Enable your app to store and retrieve user files from cloud storage with the permissions model that suits your purpose. Mobile Hub User Data Storage deploys and configures cloud storage buckets using Amazon Simple Storage Service (Amazon S3).
Follow the steps listed here to configure the data storage feature to the app. So now Update your app with the latest copy of the cloud configuration file that has to be downloaded after the user data storage has been configured.
Till here we are able to configure the required set-up for AWS SDK and as well setting-up the app in AWS mobile hub and using the cloud configuration file.
Let’s start working on the application to save, list and delete the images in the s3 bucket we had created in the above process.
- Xcode 7 and later
- iOS 8 and later
1. Open Xcode and select a single view application and name it as you like. For the sample i use as s3sample.
2. There are three ways to import the AWS Mobile SDK for iOS into your project:
You should use one of these three ways to import the AWS Mobile SDK but not multiple. Importing the SDK in multiple ways loads duplicate copies of the SDK into the project and causes compiler errors.
3. Let’s use Carthage to set-up the SDK.
4. Install the latest version of Carthage, navigate to your project folder and create the Cartfile and add the following to your Cartfile that is created in your project:
5. Then run the following command:
$ carthage update
6. Now open the project in Xcode, select your Target. Under General tab, find Embedded Binaries and then click ‘+’ button.
7. Click the Add Other… button, navigate to the AWS<#ServiceName#>.framework files that are listed under Carthage -> build -> iOS and select them. Do not check the Destination: Copy items if needed checkbox when prompted.
8. Under the Build Phases tab in your Target, click the + button on the top left and then select New Run Script Phase. Then setup the build phase as follows. Make sure this phase is below the Embed Frameworks phase.
9. Running the following command in your project directory with Carthage will automatically pickup the new changes if any in SDK.
$ carthage update
10. Add the following imports to the classes that perform user data storage operations:
11. Add the following code to your AppDelegate to establish a run-time connection with AWS Mobile.
Let’s see how to upload an image file from device gallery to Amazon S3 bucket
In XCode, open the storyboard and in viewcontroller interface, add a button to open the device gallery, an imageview to show the selected image and as well another button to upload that image and set the respective actions and outlets for the buttons and imageview.
Write the following code.
To open the device gallery
To set the selected image to imageview using the imagepicker delegate method. Extend the image picker and navigation controller delegate to get the image picker delegate method assigned to the viewcontorller.
And To upload the image to S3 bucket using the AWS Sdk methods.
For upload button Action
In the above method, near “BUCKET NAME” specify the bucket name you had created in AWS and as well let the key be your image name. The image name should be unique. Else if we use the same image name, it will get replaced in s3 bucket.
So, it’s better we use the current time in milliseconds to be unique. The KEY plays the important role as we can identify the images only with that KEY.
At this point you can run the application and see app showing the interface. Select the image and upload that to S3. Once you get success log, you can go to the S3 in AWS console and check the image there.
1. If your image is not appeared in the S3 bucket, then it might be due to some S3 policies issues. You can check the details about policies here. Policies are nothing but the permissions that we have to give to the bucket. So for now let’s go with the default policy which will give the read and write access to the bucket.
2. And one important thing you should remember is about the region of S3 bucket. We need to have the S3 bucket in the same region of your account or the region that cognito pool id is generated. (you can check this in the AWS SDK configuration steps that are listed above and in the “awsconfiguration.json” that is generated while setting up the app in AWS mobile hub). It should be mostly “USEAST-1” which is (US-EAST (N.Virginia)).
Once the above configuration is done correctly, re-run the app and upload an image. And now you can see the image in S3 bucket. Incase if you are still not able to see the image please repeat the above steps again and see if anything is wrong. Mainly don’t forget about the S3 bucket policies and region.
Listing the images from S3
Add another Swift class to the Xcode project and set-up the UITableview in that class in storyboard or via programmatically to list the images that are stored in the S3 bucket. Lets name it as “ImagesListView.Swift” for now.
Add a bar button in the ViewController class where we had set-up image upload and set the segue for that barbutton to show this list.
In “ImagesListView.Swift”, lets add the following code to get the images list. Define an array of any object to store the images list and load in tableview and add the following method to get the list of objects from S3 bucket.
To get this method working, we have to update our info.plist file with the following options.
Add the following code.
Add the following code in respective tableview delegate & datasource methods
As here we need to load the image from remote url, we need to use Async image loading. Add the following extension at the bottom of the class.
Run the code to see the images that are uploaded to your s3 bucket.
Let’s see how to delete the images from s3 bucket.
In “ImagesListView.Swift”, lets add a bar button to enable the tableview editing and deleting the selected image from the list and as well from S3 bucket. Write the following code for bar button and respective action.
Once the “Edit” is clicked, the tableview will be in edit mode with default delete buttons. You can select any image and delete it. So to get that image deleted from list and as well from s3 bucket write the following code.
Tableview Delegate method:
And Delete image from bucket:
You can run the app and if everything is done correctly as listed above, then you can upload the images, List the images and Remove the images to and from your AWS S3 bucket.
The final output:
Keval Padia is the founder & CEO of Nimblechapps, a fast-growing mobile app development company. The current innovation and updates of the field lures him to express his views and thoughts on certain topics.