We'll be convering following things:
- Connection with AWS via terminal using
Amplify
- Add
Storage
plugin/category - Fetch complete list in bucket
- Download any object using
key
Official Statement by AWS docs
Note
For new projects, we recommend using the latest v2 version of AWS Amplify Library for Android to quickly implement common app use cases like Authentication, Storage, Push Notifications and more.
- An AWS console
- Create a user in AWS Console and get following.
- AWS Console Login:
- AccountId
- UserName
- Password
- S3 Bucket Keys:
- Access Key
- Secret Key
- AWS Console Login:
We'll be using amplify-sdk docs for AWS setup
android {
compileOptions {
// Support for Java 8 features
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// Amplify core dependency
implementation 'com.amplifyframework:core:ANDROID_VERSION'
// Support for Java 8 features
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
- Sync project files
Install Amplify CLI docs
- Open cmd in window (as administrator)
- Type
curl -sL https://aws-amplify.github.io/amplify-cli/install-win -o install.cmd && install.cmd
- Verify, if amplify has successfully installed, by typing
amplify _v
- In terminal, open you android studio project path as "C:\Users\HP\AndroidStudioProjects\MyAmplifyApp>"
- Initialize Amplify
amplify init
- Enter the following when prompted:
? Enter a name for the project
`MyAmplifyApp`
? Initialize the project with the above configuration?
`No`
? Enter a name for the environment
`dev`
? Choose your default editor:
`Android Studio`
? Choose the type of app that you're building
`android`
? Where is your Res directory:
`app/src/main/res`
? Select the authentication method you want to use:
`IAM user`
? Enter credentials Access and Secret Key (to login) and region for the AWS account.
`accessKey`
`secretKey`
`region`
- Declare feature of AWS that we'll be using (e.g. storage s3_bucket)
amplify add storage
S3_bucket configuration
? Please select from one of the below mentioned services:
> Content (Images, audio, video, etc.)
NoSQL Database
? Please provide a friendly name for your resource that will be used to label this category in the project:
> mystorage
? Please provide bucket name:
> mybucket
Access permissions to bucket
? Restrict access by?
> Auth/Guest Users
Individual Groups
Both
Learn more
? Who should have access:
❯ Auth users only
Auth and guest users
? What kind of access do you want for Authenticated users?
◯ create/update
> ◉ read
◯ delete
? What kind of access do you want for Guest users?
◯ create/update
> ◉ read
◯ delete
? Do you want to add a Lambda Trigger for your S3 Bucket? (y/N)
-> N
Finally push all changes
amplify push
This will download amplifyConfiguration.json
and awsconfiguration.json
in res/raw folder.
Once done, you have successfully establish connection with AWS server.
Add following dependency
dependencies {
implementation 'com.amplifyframework:aws-storage-s3:ANDROID_VERSION'
implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
}
Add following plugins in appClass
try {
val config = AmplifyConfiguration.fromConfigFile(applicationContext, R.raw.amplifyconfiguration)
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.addPlugin(AWSS3StoragePlugin())
Amplify.configure(config, applicationContext)
Log.i(TAG, "App: Amplify -> Initialized Amplify")
} catch (error: AmplifyException) {
Log.e(TAG, "App: Amplify -> Could not initialize Amplify", error)
}