Skip to content

Commit

Permalink
refactor(S3FileStorage): Consistenly use runCatching
Browse files Browse the repository at this point in the history
Avoid to suppress exceptions by consistently using `runCatching`.
Trivially reduce nesting and improve a log message while at it.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Oct 19, 2023
1 parent 8f7d0ad commit f93a9ac
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions utils/ort/src/main/kotlin/storage/S3FileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import software.amazon.awssdk.services.s3.model.S3Exception
* blocking, but the [write] operation is asynchronous unless a [customEndpoint] is provided. Contents are compressed
* before store if the [compression] flag is set to true.
*/
@Suppress("SwallowedException")
class S3FileStorage(
/** The AWS access key */
private val accessKeyId: String? = null,
Expand Down Expand Up @@ -106,13 +105,13 @@ class S3FileStorage(
bucket(bucketName)
}.build()

return try {
return runCatching {
val response = s3Client.getObjectAsBytes(request)
val stream = ByteArrayInputStream(response.asByteArray())
if (compression) XZCompressorInputStream(stream) else stream
} catch (e: NoSuchKeyException) {
throw NoSuchFileException(File(path))
}
}.onFailure { exception ->
if (exception is NoSuchKeyException) throw NoSuchFileException(File(path))
}.getOrThrow()
}

override fun write(path: String, inputStream: InputStream) {
Expand All @@ -121,20 +120,20 @@ class S3FileStorage(
bucket(bucketName)
}.build()

inputStream.use {
val body = inputStream.use {
if (compression) {
val stream = ByteArrayOutputStream()
XZCompressorOutputStream(stream).write(it.readBytes())
RequestBody.fromBytes(stream.toByteArray())
} else {
RequestBody.fromBytes(it.readBytes())
}
}.let {
try {
s3Client.putObject(request, it)
} catch (e: S3Exception) {
logger.warn { "Can not write $path to S3 bucket $bucketName" }
}
}

runCatching {
s3Client.putObject(request, body)
}.onFailure { exception ->
if (exception is S3Exception) logger.warn { "Can not write '$path' to S3 bucket '$bucketName'." }
}
}
}

0 comments on commit f93a9ac

Please sign in to comment.