From 4e2729ae26d40c3a9ab3072828decebb80f6b446 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 9 May 2019 07:41:50 +0100 Subject: [PATCH] [reactives3client] handle key not found --- .../net/kemitix/s3thorp/ReactiveS3Client.scala | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/scala/net/kemitix/s3thorp/ReactiveS3Client.scala b/src/main/scala/net/kemitix/s3thorp/ReactiveS3Client.scala index fd55068..9ba95d3 100644 --- a/src/main/scala/net/kemitix/s3thorp/ReactiveS3Client.scala +++ b/src/main/scala/net/kemitix/s3thorp/ReactiveS3Client.scala @@ -1,8 +1,9 @@ package net.kemitix.s3thorp +import cats.effect.IO import com.github.j5ik2o.reactive.aws.s3.S3AsyncClient import com.github.j5ik2o.reactive.aws.s3.cats.S3CatsIOClient -import software.amazon.awssdk.services.s3.model.HeadObjectRequest +import software.amazon.awssdk.services.s3.model.{HeadObjectRequest, NoSuchKeyException} import software.amazon.awssdk.services.s3.{S3AsyncClient => JavaS3AsyncClient} class ReactiveS3Client extends S3Client { @@ -14,9 +15,12 @@ class ReactiveS3Client extends S3Client { .bucket(bucket) .key(key) .build() - for { - response <- s3Client.headObject(request) - // TODO catch 404 error when key doesn't exist - } yield Some((response.eTag(), response.lastModified())) + try { + for { + response <- s3Client.headObject(request) + } yield Some((response.eTag(), response.lastModified())) + } catch { + case _: NoSuchKeyException => IO(None) + } } }