Created
October 17, 2020 00:37
-
-
Save Lucas2k/d8c7a585c24ff5d5068b6abf7a2b6970 to your computer and use it in GitHub Desktop.
Subir archivos a un servidor con Minio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UploadTask(val context: Context, | |
val file : File, | |
val objectStoreEndpoint: String, | |
val objectStorePort: String , | |
val objectStoreAccessKey: String , | |
val objectStoreSecretKey: String , | |
val objectStoreBucket: String , | |
val objectSignerOverride: String ) : AsyncTask<Any, Any, Any>() | |
{ | |
private var onListener: UploadListener? = null | |
fun setOnUploadListener(listener: UploadListener){ | |
this.onListener = listener | |
} | |
override fun onPreExecute() { | |
super.onPreExecute() | |
} | |
override fun doInBackground(vararg params: Any?) { | |
val endpoint = "http://$objectStoreEndpoint:$objectStorePort" | |
val accessKey = objectStoreAccessKey | |
val secretKey = objectStoreSecretKey | |
val credentials = BasicAWSCredentials(accessKey, secretKey) | |
val clientConfiguration = ClientConfiguration() | |
clientConfiguration.signerOverride = objectSignerOverride //"AWSS3V4SignerType" | |
val options = S3ClientOptions.builder() | |
.setPathStyleAccess(true) | |
.build() | |
val s3Client = AmazonS3Client(credentials) | |
s3Client.endpoint = endpoint | |
s3Client.setS3ClientOptions(options) | |
try { | |
if (!s3Client.doesBucketExist(objectStoreBucket)) { | |
s3Client.createBucket(objectStoreBucket) | |
} | |
/* val buckets: List<Bucket> = s3Client.listBuckets() | |
for (bucket in buckets) { | |
Log.e( | |
"Bucket ", | |
"Name " + bucket.name.toString() + " Owner " + bucket.owner | |
.toString() + " Date " + bucket.creationDate | |
) | |
}*/ | |
val transferUtility = TransferUtility.builder() | |
.context(context) | |
.s3Client(s3Client) | |
.build() | |
val observer: TransferObserver = transferUtility.upload( | |
objectStoreBucket, | |
file.name, | |
file, | |
CannedAccessControlList.PublicRead | |
) | |
observer.setTransferListener(object : TransferListener { | |
override fun onStateChanged(id: Int, state: TransferState?) { | |
if (state === TransferState.COMPLETED) { | |
observer.cleanTransferListener() | |
Log.e("UPLOAD COMPLETED", id.toString() + state!!.name) | |
Analytics.trackEvent("UPLOAD COMPLETED " + id.toString() + state!!.name) | |
onListener?.onStateChanged(id, state) | |
} | |
} | |
override fun onProgressChanged( | |
id: Int, | |
bytesCurrent: Long, | |
bytesTotal: Long | |
) { | |
} | |
override fun onError(id: Int, exception: Exception) { | |
observer.cleanTransferListener() | |
Log.e("UPLOAD ERROR", exception.printStackTrace().toString()) | |
Crashes.trackError(exception) | |
onListener?.onError(id, exception) | |
} | |
}) | |
}catch (exception: Exception){ | |
Log.e("UPLOAD ERROR", exception.printStackTrace().toString()) | |
Crashes.trackError(exception) | |
onListener?.onError(1, exception) | |
} | |
} | |
override fun onPostExecute(result: Any?) { | |
super.onPostExecute(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment