Android-Amplify: Uploading/downloading file to/from AWS S3 using Amplify

5.7k views Asked by At

I want to develop a simple android app to upload an image file to an already created S3 bucket in AWS. If I google, All the latest AWS documentations are redirecting me to use Amplify framework. I don't understand the documentation of uploading a file described here. I don't understand where I can provide bucket name, IAM credentials, etc. I don't find any video tutorials as well. Why AWS if forcing to use Amplify without providing proper documentation?

Here they mention to configure all details in Amplify CLI using

amplify add storage

And ask to push changes using

amplify push

But if we want to add details such as bucket name, user details, etc. programmatically, then how do we do it? Give me step by step details either using Amplify framework or old AWS SDK for android for uploading file without Cognito

2

There are 2 answers

0
Jameson On

There are a number of ways to upload files to S3, from an Android device. Here are a few.

Using Amplify Android

The main documentation for Amplify Android's Storage category is written with the assumption that you'll create new AWS resources, using the Amplify CLI. There are also some note about using an existing S3 bucket.

Using the AWS SDK for Android

If neither meet your needs, you can use the old TransferUtility from the AWS SDK for Android. Here's an example use of the TransferUtility.

AWS SDK for Android, No Cognito

As you note, the documentation above uses the AWSMobileClient, which is an interface to Amazon Cognito. However, you can use any implementation of the CredentialsProvider, for authentication; AWSMobileClient is just one example of a credentials provider.

The simplest (and a least secure) approach might be to provide an IAM user's access and secret key using a StaticCredentialsProvider, as below.

val region = Region.getRegion(Regions.US_EAST_1)
val credentials = BasicAWSCredentials(accessKey, secretKey)
val provider = StaticCredentialsProvider(credentials)

val transferUtility = TransferUtility.builder()
    .context(applicationContext)
    .s3Client(AmazonS3Client(provider, region))
    .awsConfiguration(AWSConfiguration(applicationContext))
    .build()

val listener = object: TransferListener {
    override fun onProgressChanged(id: Int, curr: Long, tot: Long) {}
    override fun onStateChanged(id: Int, state: TransferState?) {
        when (state) {
            COMPLETED -> { Log.i("Demo", "Upload succeeded.") }
            FAILED -> { /* handle err */ }
            else -> { /* handle cases... */ }
        }
    }
    override fun onError(id: Int, ex: Exception?) { /* handle err */ }
}

transferUtility.upload(remoteBucket, remoteKey, localFile)
    .setTransferListener(listener)
0
Ashok Raj On

Aws - amplify for android, Using this we can perform aws operations, here i am upload the files into s3 bucket.

let me share the basic documents needed for implementing this

for basic amplify setups:

  1. https://www.youtube.com/watch?v=vefyYGn9C00&t=2s
  2. https://www.youtube.com/watch?v=qgrXMzFMHx4&t=306s

step 1:

https://docs.amplify.aws/lib/auth/getting-started/q/platform/android/

Authentication

step 2:

s3- storage

step 3:

  1. https://docs.amplify.aws/lib/storage/getting-started/q/platform/android/ (You can create bucket from terminal)
  2. https://docs.amplify.aws/cli/storage/import/ (If you have already bucket created in s3 console follow this document)
  3. https://docs.amplify.aws/lib/storage/configureaccess/q/platform/android/ ( you must add StorageUploadFileOptions before uploading the file)