Android-amplify: Uploading/downloading File To/from Aws S3 Using Amplify
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:
step 1:
https://docs.amplify.aws/lib/auth/getting-started/q/platform/android/
Authentication
step 2:
- https://docs.amplify.aws/lib/auth/getting-started/q/platform/android/
- https://docs.amplify.aws/lib/auth/signin/q/platform/android/
s3- storage
step 3:
- https://docs.amplify.aws/lib/storage/getting-started/q/platform/android/ (You can create bucket from terminal)
- https://docs.amplify.aws/cli/storage/import/ (If you have already bucket created in s3 console follow this document)
- 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"