Kotlin Android Amazon S3 – upload/download files (images)

Amazon Simple Storage Service (Amazon S3) is object storage built to store and retrieve any amount of data from web or mobile. Amazon S3 is designed to make web-scale computing easier for developers. In this tutorial, we’re gonna create an Android App that can upload/download files (images) to/from Amazon S3 with Kotlin language.

Related Post: How to integrate AWS Mobile SDK into Android App

I. Technology

– Android Studio 2.x
– AWS Mobile SDK Client 2.6.7

II. Data Storage with Amazon S3

1. Integrate AWS Mobile SDK into Android App

Please visit this article for details.

2. Enable User Data Storage

Open your project in Mobile Hub and choose the User Data Storage tile to enable the feature.

amazon-s3-storage-enable

Choose Store user data and click on Save button:

amazon-s3-storage-enable-save

3. Updated latest cloud configuration file

Return to the project details page, click on Integrate button:

amazon-s3-storage-integrate

Download new Cloud Config file, then override it in <project>/app/src/main/res/raw:

amazon-s3-storage-upadte-config-file

4. Create an IAM user

We need to provide access permission mobile bucket. So follow these step to create an IAM user and get Access key ID and Secret access key:

Go to https://console.aws.amazon.com/iam/
In the navigation pane, choose Users and then choose Add user.

springboot amazon s3 starter - choose user

Input User name, choose Programmatic access for Access type:

amazon s3 starter - add user info

Press Next: Permissions button -> go to Set permissions for jsa-user screen.
Now, choose Attach existing policies directly -> filter policy type s3, then check AmazonS3FullAccess:

amazon s3 starter - add policies

Press Next: Review:

amazon s3 starter - review policies

Press Create user:

Press Download .csv for {Access key ID, Secret access key}.

5. Connect to Amazon S3
5.1 Add dependencies

Open app/build.gradle, add:

5.2 Add TransferService

Open AndroidManifest.xml, add service and set permission for read/write file:

5.3 Establish connection with AWS Mobile

AWSMobileClient is a singleton that will be an interface for AWS services. In onCreate() method, initialize it:

5.4 Upload a File

5.5 Download a File

III. Practice

1. Set up Project

Follow instruction above to:
– Integrate AWS Mobile SDK into Android App
– Enable User Data Storage
– Updated latest cloud configuration file
– Create an IAM user and get {Access key ID, Secret access key}

Project Structure:

kotlin-android-amazon-s3-structure

2. Dependencies

Open app/build.gradle, add:

3. AndroidManifest.xml

Open AndroidManifest.xml, add service and set permission for network, read/write file:

4. Layout

amazon-s3-storage-layout

5. Main Code

MainActivity.java

6. Check Results

Run the Android App, click on CHOOSE FILE button, select image file and set file name in the blank. Then check UPLOAD and DOWNLOAD.

amazon-s3-storage-demo

Go to https://console.aws.amazon.com/s3/buckets/, click on xxx-userfiles-mobilehub-xxx bucket (which is automatically generated when we enable User Data Storage:

amazon-s3-storage-bucket-result-1

We will see jsaS3 folder:

amazon-s3-storage-bucket-result-2

And our files have been uploaded:

amazon-s3-storage-bucket-result-3

IV. Sourcecode

KotlinS3Amazon

By JavaSampleApproach | January 28, 2018.

Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*