Skip to content

Instantly share code, notes, and snippets.

@mand2
Created September 2, 2021 14:47
Show Gist options
  • Save mand2/26a3c01cf3f14d2f747e50bc5bb76d7f to your computer and use it in GitHub Desktop.
Save mand2/26a3c01cf3f14d2f747e50bc5bb76d7f to your computer and use it in GitHub Desktop.
@JvmField val pathRegEx: Regex = Regex("^(/)|(/)$")
fun uploadDirectoryOfClient(s3DirectoryClient:TransferManager, defaultPath: String) {
// 로컬 디렉토리 세팅
val localPath = "/d/test/uploader/"
val localDirectory = File(localPath)
// s3 객체 경로 세팅
val bucketName = "mand2"
val s3Path = pathRegEx.replace(defaultPath, "")
val s3UploadPath = "$s3Path/upload/210831"
var uploadProgressBar = StringBuffer() // 업로드 진행상황 log
try {
// 실제 업로드
val uploadDirectory: MultipleFileUpload = s3DirectoryClient.uploadDirectory(
bucketName,
s3UploadPath,
localDirectory,
true
)
uploadProgressBar
.append("[Running] upload progressing... start")
.appendLine()
while (!uploadDirectory.isDone) {
try {
Thread.sleep(1000)
} catch (e: InterruptedException) {
return;
}
val progress: TransferProgress = uploadDirectory.progress
val pct: Double = progress.percentTransferred
val pctFormat = DecimalFormat("##0.00")
uploadProgressBar
.append("[Running] ${pctFormat.format(pct)}% upload progressing...")
.appendLine()
}
logger.info("[Uploader] Upload Directory to S3 Success !")
} catch (e: AmazonServiceException) {
logger.error("Amazon service error: {}, {}", e.message, e)
} catch (e: AmazonClientException) {
logger.error("Amazon client error: {}, {}", e.message, e)
} catch (e: InterruptedException) {
logger.error("Transfer interrupted: {}, {}", e.message, e)
} finally {
s3DirectoryClient.shutdownNow()
logger.info(uploadProgressBar.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment