You have to use Amazon Cognito Identity Provider to obtain AWS credentials. Those credentials you can use in your mobile application to access AWS services. You can also set user-specific permissions to access particular AWS services. You don't have to embed personal credentials. Amazon Cognito will be covered in more detail in Chapter 6, User Authentication with AWS Cognito.
So far, we have covered AWS SDK for IoT and AWS Mobile SDK for Android. Let's explore an example for the Transfer Utility to consume Amazon S3 services. Here we will upload a file from a mobile device and download a file to a mobile device. We will use Android Studio, Amazon Cognito, Amazon S3, and Amazon IAM. Please perform the following steps:
- Start Android Studio and create a new project. Add the required information and click Next:
- Select the Target Android Devices. Here I have selected Phone and Tablet and the API version is 15, which supports 100% of devices:
- Select the activity as per your project needs:
- In the Configure Activity screen, you can change the Activity Name and Layout Name or you can keep them as they are:
- You can see the following screen after successfully creating the project:
- Open the app/build.gradle file and add the following modules as dependencies for the AWS Mobile SDK:
compile 'com.amazonaws:aws-android-sdk-core:2.6.6'
compile 'com.amazonaws:aws-android-sdk-cognito:2.6.6'
compile 'com.amazonaws:aws-android-sdk-s3:2.6.6'
- Amazon S3 will transfer files using the TranferUtility service. For that, open the app/manifests/AndroidManifest.xml file and add the TransferUtility service in the application:
<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />
- Add the following permissions under the manifest tag, which will give you permission to upload and download files from the internet through Android devices:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_DOWN"/>
<uses-permission android:name="android.permission.SMARTBONDING_FEATURE_ENABLED" />
- To access Amazon services from your mobile applications, you have to configure the AWS credentials. Amazon Cognito is used as the credential provider. You have to create the identity pool under the Federated Identities in Amazon Cognito and provide the IAM role. You have to create two roles, one for authenticated users and another for unauthenticated users, and provide the following policy. We will cover user authentication with Amazon Cognito in more detail in Chapter 6, User Authentication with AWS Cognito:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "Stmt1510936216000",
"Effect": "Allow",
"Action": ["s3:*"],
"Resource": ["arn:aws:s3:::<Bucket_Name>/*"]
}]
}
- To enable file upload and download to and from S3, we need to create a button and add an onClick event. You have to add the following code into your acitvity_main.xml file:
<Button
android:id="@+id/upload_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Upload to S3"
android:onClick="uploadFile"/>
<Button
android:id="@+id/downaload_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Download from S3"
app:layout_constraintLeft_toRightOf="@id/upload_file"
android:onClick="downloadFile"/>
- You need to add following imports in the MainActivity.java file to use the Amazon Cognito, Amazon S3, and TransferUtility services:
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
- You need to create an instance of S3 and TransferUtility. You need to specify the file path for upload and download:
AmazonS3 s3Client;
TransferUtility transferUtility;
File uploadFilePath = new File(<FILE_UPLOAD_PATH>);
File downloadFilePath = new File(<FILE_DOWNLOAD_PATH);
- The onCreate method will initialize the activity. Add the following method for Cognito credentials and Transfer Utility:
getCognitoCredentials();
createTransferUtility();
- The following method will create Cognito credential providers. You can pass the Android context, Identity Pool, and region to create the instance:
Public void getCognitoCredentials(){
CognitoCachingCredentialsProvider credentials
= new CognitoCachingCredentialsProvider(
getApplicationContext(),
<Identity_Pool_ID>,
Regions.<Your_Cognito_IdentityPool_Region>
);
createS3Client(credentials);
}
- The following method will create the Amazon S3 client where you have to pass Cognito credentials and set your bucket region:
public void createS3Client(CognitoCachingCredentialsProvider credentials){
s3 = new AmazonS3Client(credentials);
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
}
- The following method will create a Transfer Utility instance. Note that TransferUtility is used to upload a single file in multiple parts using multiple threads. It is useful for uploading large files mentioning the file path than stream:
public void createTransferUtility(){
transferUtility = new TransferUtility(s3, getApplicationContext());
}
- The following method will be used to upload files from transferUtility's upload. You have to specify the bucket name, filename, and upload file path:
public void uploadFile(View view){
TransferObserver transferObserver = transferUtility.upload(
"<S3_Bucket_Name>",
"<Upload_File_Key_Name>",
uploadFilePath
);
}
- The following method will be used to download files from transferUtility's download. You have to specify the bucket name, filename, and download file path:
public void downloadFile(View view){
TransferObserver transferObserver = transferUtility.download(
"<S3_Bucket_Name>",
"<Download_File_Key_Name>",
downloadFilePath
);
}
- You will see the following screen on your mobile device after successfully running the application:
When you tab on FILE UPLOAD TO S3, if the file is uploaded successfully, you can see it in your S3 bucket from the console:
 When you click on FILE DOWNLOAD FROM S3, if the file is downloaded successfully, you can see it in your folder or path:Â