Skip to content

Instantly share code, notes, and snippets.

@yomajkel yomajkel/post.scala
Created Sep 24, 2018

Embed
What would you like to do?
the description for this gist
case class S3UploaderException(msg: String) extends Exception(msg) with NoStackTrace
object S3ClientService extends S3ClientService
trait S3ClientService extends S3Conf {
private val s3Client: AmazonS3 = new AmazonS3Client(credentialsProvider.getCredentials())
def upload(filePath: Path): Future[String] = {
val fileName = filePath.getFileName().toString()
val key = s"temp/${fileName}"
val file: File = new File(filePath.normalize().toString())
val promise = Promise[String]()
val listener = new FileUploadProgressListener(key, promise)
val request = new PutObjectRequest(bucketName, key, file)
request.setGeneralProgressListener(listener)
s3Client.putObject(request)
promise.future
}
class FileUploadProgressListener(key: String, promise: Promise[String]) extends ProgressListener {
override def progressChanged(progressEvent: ProgressEvent): Unit = progressEvent.getEventType match {
case ProgressEventType.TRANSFER_FAILED_EVENT =>
promise.failure(S3UploaderException(s"Uploading a file with a key: $key"))
case ProgressEventType.TRANSFER_COMPLETED_EVENT | ProgressEventType.TRANSFER_CANCELED_EVENT =>
promise.success(key)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.