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

Sign Up

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

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.

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.

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. 😅

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

Set function name and choose language — Python 3.9

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

Goto action tab and create Resources

Setup resource name and tap create resource

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

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

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

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

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

Back to Resources → Actions → Deploy API

Finally, you will get the 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 🍻

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashok

Ashok

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