How to Connect Android Application with Database in Server, Amazon EC2

  1. Set Up the AWS Mobile SDK for Android (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/setup.html)
    1. I used Android Studio and added the aws-android-sdk-core dependency to my app/build.gradle file, along with the dependencies for the individual services that my project will use, as shown below.
      dependencies {
          compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
          // In the document, they used s3 but I used ec2
          compile 'com.amazonaws:aws-android-sdk-ec2:2.2.+'
          compile 'com.amazonaws:aws-android-sdk-ddb:2.2.+'
          // If you don't add ddb-mapper, compiler can't find         com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper which will be imported later.
          compile 'com.amazonaws:aws-android-sdk-ddb-mapper:2.2.+'
      }
      
  2. Where Should I place AWS Credentials? I referred here.
    (https://www.numetriclabz.com/integrate-amazon-s3-to-android-tutorial/)

    public class MainActivity extends AppCompatActivity {
        AmazonS3 s3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // callback method to call credentialsProvider method.
            credentialsProvider();
        }
    
        public void credentialsProvider(){
            // Initialize the Amazon Cognito credentials provider
            CognitoCachingCredentialsProvider credentialsProvider = new     CognitoCachingCredentialsProvider(
                getApplicationContext(),
                us-east-1:dbacd6aa-9393-475e-b687-xxxxxxxx;, // Identity Pool ID
                Regions.US_EAST_1 // Region
            );
            setAmazonS3Client(credentialsProvider);
        }
    /**
    *  Create a AmazonS3Client constructor and pass the credentialsProvider.
    * @param credentialsProvider
    */
        public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider) {
            // Create an S3 client
            s3 = new AmazonS3Client(credentialsProvider);
            // Set the region of your S3 bucket
            s3.setRegion(Region.getRegion(Regions.US_EAST_1));
        }
    }
    
    
  3. Store and Retrieve App Data in Amazon Dynamo DB
    (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/dynamodb_om.html)

    1. Create a DynamoDB Table
      I couldn’t find a way to add attributes when I created a table.
      Then I should add them whenever I add new items.
    2. Set Permissions
      1. Log in to the IAM Console.
      2. Select Roles and select the “Unauth” role that Cognito created for you.
      3. Click Edit Policy in Inline Policies.
      4.  Enter a name for your policy and paste in the policy document shown the document, replacing the Resource values with the ARNs for your table and index. (You can retrieve the table ARN from the Details tab of database; then append /index/* to obtain the value for the index ARN.
      5. Click Apply Policy.
    3. To establish mappings, DynamoDB defines annotations.
    4. Specify the region when you create your AmazonDynamoDBClient.
      (https://stackoverflow.com/questions/34223123/cognito-dynamodb-not-authorized-to-perform-dynamodbupdateitem-on-resource)
    5.  Interacting with stored objects is synchronous and must be taken off of the main thread.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s