How to integrate AWSS3 in your ios swift application. Generating pre signed url to access secure files.

Madhur Jain
Nov 4 · 2 min read

Today we will learn about how to use Amazon web services in ios to get pre signed URL to access the private files of the Amazon server.

Install pod in the project as following
pod ‘AWSS3’

After successful installation, we have to do few changes in Info.plist

The above mention code is for App transport security , we have to allow our app to communicate via URLs from the third party as mentioned in the code we have allowed www.amazon.com for data transactions.

Now there are two ways to setup AWS in your application as follows
1.Amazon Cognito Mode
2.Open Mode

You can use any one of them, In case of Cognito mode you have required your identityPoolId and AWS server region to access your Cognito id to generate a coginto mode URL to access private files.

To setup AWS we have to do something in AppDelegate.swift

In case of Open Mode, you have required three keys to generate a presigned URL to access private files of amazon server one is access key and another one is secret key and AWS server region.

As shown in code snippet above setup of AWS in ios using swift3 both ways.

Import AWSS3 and Copy the above method and paste it in your AppDelegate.swift and call setUpAWS() in didFinishLaunchingWithOptions

Now your AWS setup is completed in your project now all you have to do is for generating a Presigned URL

Create a new swift file in your project and copy paste the following code in the file.

import UIKit
import Foundation
import AWSS3

let S3BucketName = "demo_bucketName"
class AWSService {
var preSignedURLString = ""

func getPreSignedURL( S3DownloadKeyName: String)->String{
let getPreSignedURLRequest = AWSS3GetPreSignedURLRequest()
getPreSignedURLRequest.httpMethod = AWSHTTPMethod.GET
getPreSignedURLRequest.key = S3DownloadKeyName
getPreSignedURLRequest.bucket = S3BucketName
getPreSignedURLRequest.expires = Date(timeIntervalSinceNow: 3600)

AWSS3PreSignedURLBuilder.default().getPreSignedURL(getPreSignedURLRequest).continueWith { (task:AWSTask<NSURL>) -> Any? in
if let error = task.error as NSError? {
print("Error: \(error)")
return nil
}
self.preSignedURLString = (task.result?.absoluteString)!
return nil
}
return self.preSignedURLString
}

}

The function written in the class returns a presigned URL corresponding to bucket name defined above the class and downloadKey passed in the function.

Replace the bucket name as with yours if it is dynamic then make it is as functions parameter.

The below line will get you your required signed URL of your private file kept on Amazon server.

let signedUrl = URL(string:AWSService().getPreSignedURL(S3DownloadKeyName: "demo_key")

Madhur Jain

Written by

ios and android dev.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade