Android and AWS Mobile SDK

Amazon Web Service (AWS) is an household name. So, this blog post is not about how much sense AWS makes. This is all about how you can use AWS Mobile SDK in your Android application. I feel it’s more convenient for me compared to using the REST APIs.

For this tutorial, I am going to demonstrate how to store and retrieve images from your Amazon s3 bucket using the SDK. This tutorial assumes you have an AWS account.

The first thing to do is to include the needed SDKs in your project. To achieve this, we add the following dependencies.

dependencies {
    compile 'com.amazonaws:aws-android-sdk-core:2.+' 
    compile 'com.amazonaws:aws-android-sdk-s3:2.+'
}

It is not encouraged to embed credentials in the application, we need to get an IDENTITY POOL ID using the Amazon Cognito Identity service. You can get that done by following the steps described in the documentation.

After this is done, we can gallantly continue to the Android implementation.

First, we update our manifest file to request for some permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

You can insert the following snippet where you need to trigger the event that handles the connection to AmazonClient, creation and retrieval of buckets and objects.

    String filePath = "PATH_TO_FILE"; // the path of the image/path you want to upload.
    File file = new File(filePath);

    String MY_PICTURE_BUCKET = "BUCKET_NAME"; //the name of the bucket you want to create. This  name must be unique

    String MY_IDENTITY_POOL_ID = "IDENTITY_POOL_ID"; //the id you just got from cognito console.
                        
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                                    MainActivity.this, // Application Context
                                    MY_IDENTITY_POOL_ID, // Identity Pool ID
                                    Regions.US_EAST_1 // Region enum
                            );
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    if (!s3Client.doesBucketExist(MY_PICTURE_BUCKET)) { //bucket names must be unique.
            s3Client.createBucket(MY_PICTURE_BUCKET);
    }
    String MY_PICTURE_NAME = file.getName();
//This will upload the file with the fileName
    PutObjectRequest por = new PutObjectRequest(MY_PICTURE_BUCKET, MY_PICTURE_NAME, file);
    if (!s3Client.doesObjectExist(MY_PICTURE_BUCKET, MY_PICTURE_NAME)) { //object name must be unique.
            s3Client.putObject(por);
    }
    
    //This will fetch the object with the fileName in the bucketName. This snippet assumes that the file coming is an image.
    GetObjectRequest gor = new GetObjectRequest(bucketName, fileName);
        Bitmap bmp = null;
        if (s3Client.doesObjectExist(bucketName, fileName)) {
            try {
                S3Object getResponse = s3Client.getObject(gor);
                InputStream is = getResponse.getObjectContent();
                bmp = BitmapFactory.decodeStream(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
                

Be sure not to run the code on the main UI thread.

If you have done everything right, you should be happy with the results right now. Feel free to leave a comment if there is a need to.