Skip to content Skip to sidebar Skip to footer

Android-amplify: Uploading/downloading File To/from Aws S3 Using Amplify

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 Ampl

Solution 1:

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 {
    overridefunonProgressChanged(id: Int, curr: Long, tot: Long) {}
    overridefunonStateChanged(id: Int, state: TransferState?) {
        when (state) {
            COMPLETED -> { Log.i("Demo", "Upload succeeded.") }
            FAILED -> { /* handle err */ }
            else -> { /* handle cases... */ }
        }
    }
    overridefunonError(id: Int, ex: Exception?) { /* handle err */ }
}

transferUtility.upload(remoteBucket, remoteKey, localFile)
    .setTransferListener(listener)

Solution 2:

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)

Post a Comment for "Android-amplify: Uploading/downloading File To/from Aws S3 Using Amplify"