Advertisement
hasancse1991

Untitled

Mar 23rd, 2020
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.64 KB | None | 0 0
  1. private fun uploadFileToS3Bucket(filePath: String, credential: AwsCredentialResponse): Maybe<String> {
  2.  
  3.         // from blog: http://blog.mallow-tech.com/2019/01/how-to-upload-files-from-an-android-app-to-aws-s3/
  4.         return Maybe.create { emitter ->
  5.  
  6.             TransferNetworkLossHandler.getInstance(context)
  7.  
  8.             val simpleDateFormat = SimpleDateFormat(DateUtils.FORMAT_UTC, Locale.ENGLISH)
  9.  
  10.             val expirationDate = try {
  11.                 simpleDateFormat.parse(credential.credentials.expiration)
  12.             } catch (e: Exception) {
  13.                 val calendar = Calendar.getInstance()
  14.                 calendar.add(Calendar.MINUTE, 10) // expiration time is 10 minutes from now
  15.                 Date(calendar.timeInMillis)
  16.             }
  17.  
  18.             val s3Client: AmazonS3 =
  19.                 AmazonS3Client(
  20.                     AwsCredential(
  21.                         credential.credentials.accessKeyId,
  22.                         credential.credentials.accessKeyId,
  23.                         credential.credentials.sessionToken,
  24.                         expirationDate
  25.                     ),
  26.                     getRegion(Regions.EU_WEST_1)
  27.                 )
  28.  
  29.             val file = File(filePath)
  30.  
  31.             val transferUtility =
  32.                 TransferUtility.builder().s3Client(s3Client).context(context).build()
  33.  
  34.             val transferObserver: TransferObserver = transferUtility.upload(
  35.                 credential.bucket,
  36.                 credential.keyPrefix,
  37.                 file,
  38.                 CannedAccessControlList.PublicRead
  39.             )
  40.  
  41.             transferObserver.setTransferListener(object : TransferListener {
  42.  
  43.                 override fun onStateChanged(id: Int, state: TransferState?) {
  44.  
  45.                     when (state) {
  46.  
  47.                         TransferState.IN_PROGRESS -> {
  48.  
  49.                         }
  50.                         TransferState.COMPLETED -> {
  51.  
  52.                         }
  53.                         TransferState.FAILED -> {
  54.  
  55.                         }
  56.                         else -> {
  57.  
  58.                         }
  59.                     }
  60.                 }
  61.  
  62.                 override fun onProgressChanged(
  63.                     id: Int,
  64.                     bytesCurrent: Long,
  65.                     bytesTotal: Long
  66.                 ) {
  67.                     if (bytesCurrent == bytesTotal) {
  68.                         emitter.onSuccess("${credential.bucket}/${credential.keyPrefix}/${file.name}")
  69.                     }
  70.                 }
  71.  
  72.                 override fun onError(id: Int, e: Exception) {
  73.                     emitter.onError(e)
  74.                 }
  75.             })
  76.         }
  77.  
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement