class AwsS3Sync(val activity: Activity): AsyncTask<Any, Any, Any>() {
override fun doInBackground(vararg params: Any?): Any? { // Initialize the Amazon Cognito credentials provider
val credentialsProvider =
CognitoCachingCredentialsProvider(
activity.applicationContext, // Application Context
"identity pool id", // Identity Pool ID
Regions.US_EAST_1 // Region enum,
)
val s3Client =
AmazonS3Client(credentialsProvider, Region.getRegion(Regions.US_EAST_1))
val fileToUpload: File = File(activity.filesDir, "sample.txt")
val writer = FileWriter(fileToUpload)
writer.append("Howdy World!")
writer.close()
//(Replace "MY-BUCKET" with your S3 bucket name, and "MY-OBJECT-KEY" with whatever you would like to name the file in S3)
val putRequest = PutObjectRequest(
"bucket-name",
"sample15.txt",
fileToUpload
)
val putResponse: PutObjectResult = s3Client.putObject(putRequest)
//Log.i("AWS", putResponse.)
val getRequest = GetObjectRequest("bucket-name", "sample15.txt")
val getResponse: S3Object = s3Client.getObject(getRequest)
val myObjectBytes: InputStream = getResponse.getObjectContent()
// Do what you want with the object
myObjectBytes.close()
return null
}
}