Getting started with AWS S3 — To upload image via API gateway — sample iOS app

Ashok
4 min readAug 19, 2021
AWS + Lambda + iOS

Sign Up

Step 1: Visit AWS S3 console and signup with you email ID, password and account name.

AWS S3 Console

Step 2: Fill your contact information — Full name, Phone number, Country, Address, State, Pincode.

Step 3: Add your billing informations. FYI, they will charge you 2 INR.

Step 4: Confirm your identity by using your mobile number. Once you confirmed it, you will get the verification code from AWS.

Confirm your identity

Step 5: Select your plan. For now, I’m selecting basic plan — “Basic support-Free”. Later you can change this based on your needs. Once plan selected, complete sign up.

Complete signup

Thats it! Your AWS S3 account successfully created.

Create bucket

Signin into your account. Go to amazon management console → S3. There create new bucket. Please disable Block Public Access settings for this bucket. 😅

Create bucket
Disable block all public access

Finally, you have created S3 bucket. 🙌

Bucket Permission

Select your bucket → Go to Permission tab.

Edit → Bucket policyAdd the following code inside that and save the changes.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::bucketsampleios/*"
}
]
}

Setup Lambda Funtion

Go to amazon management console → Lambda

Lambda
Create function

Set function name and choose language — Python 3.9

Setup basic information

Once successful function creation,

Now, we going to edit the lambda_function.py. Just copy paste the code and tap deploy CTA

import json
import base64
import boto3
import uuid
file_name = "content.png"
s3_path = str(uuid.uuid4()) + file_name
bucket_name = "BUCKET_NAME"
def lambda_handler(event, context):
s3 = boto3.client("s3")
get_file_content = event["content"]
decode_content = base64.b64decode(get_file_content)
s3_upload = s3.put_object(Bucket=bucket_name, Key=s3_path, Body=decode_content)
return {
"statusCode": 200,
"body": json.dumps(s3_path)
}

Setup API Gateway

Go to amazon management console → API Gateway → Get started → Rest API

Setup API name and tap create API CTA

Create new API

Goto action tab and create Resources

Actions

Setup resource name and tap create resource

Create Resource

Then, By tapping action → create Put method inside the resources and tap select button near by PUT method

Add PUT method

Add your Lambda fucntion called “uploadAPI” and save it.

Add lambda function

Go to Integration Request → Mapping templates → Add Content-Type as image/png and body →Save it

{
"content" : "$input.body"
}
Mapping Templates

Then, Go to Settings under API → Binary Media Types → Add binary media type as image/png → Save changes

Back to Resources → Actions → Deploy API

Deploy API

Finally, you will get the Invoke URL.

Invoke URL

Sample iOS app

let image = UIImage(named: "apple")!
if let imageData = image.pngData(){
let header : HTTPHeaders = ["Content-Type": "image/png"]
let url = "INVOKE_URL"
AF.upload(imageData, to: url, method: .put, headers: header)
.responseObject { (response:AFDataResponse<T>) in
switch response.result{
case .success(let value):
onSuccess(value)
case .failure(let error):
onError(error)
return
}}}

Check out my Github repo and follow me on LinkedIN.

Cheers 🍻

--

--

Ashok

 iOS and Flutter developer @TCS, Chennai. Here to share best practices learned through my experience. Reach me on www.linkedin.com/in/ashok1208