Skip to content

Commit fcf9b9d

Browse files
committed
Bite the bullet: switch 'S3' as AWS SDK v2
1 parent 2b123cc commit fcf9b9d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

common/app/services/S3.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package services
22

3-
import com.amazonaws.services.s3.model.CannedAccessControlList.{Private, PublicRead}
4-
import com.amazonaws.services.s3.model._
5-
import com.amazonaws.services.s3.{AmazonS3, AmazonS3Client}
3+
//import com.amazonaws.services.s3.model.CannedAccessControlList.{Private, PublicRead}
4+
//import com.amazonaws.services.s3.model._
65
import com.amazonaws.util.StringInputStream
76
import com.gu.etagcaching.aws.s3.ObjectId
87
import common.GuLogging
98
import conf.Configuration
109
import model.PressedPageType
1110
import org.joda.time.DateTime
1211
import services.S3.logS3ExceptionWithDevHint
12+
import software.amazon.awssdk.services.s3.model.{GetObjectRequest, NoSuchKeyException}
13+
import utils.AWSv2
1314

1415
import java.io._
1516
import java.util.zip.GZIPOutputStream
@@ -19,17 +20,17 @@ trait S3 extends GuLogging {
1920

2021
lazy val bucket = Configuration.aws.frontendStoreBucket
2122

22-
lazy private val client: AmazonS3 = AmazonS3Client.builder
23-
.withCredentials(Configuration.aws.credentials.get)
24-
.withRegion(conf.Configuration.aws.region)
25-
.build()
23+
lazy private val client = AWSv2.S3Sync
2624

2725
private def withS3Result[T](key: String)(action: S3Object => T): Option[T] = {
2826
val objectId = ObjectId(bucket, key)
2927
try {
30-
val request = new GetObjectRequest(bucket, key)
28+
val request = GetObjectRequest.builder().bucket(bucket).key(key).build()
3129
val result = client.getObject(request)
32-
log.info(s"S3 got ${result.getObjectMetadata.getContentLength} bytes from ${result.getKey}")
30+
31+
val getObjectResponse = result.response()
32+
33+
log.info(s"S3 got ${getObjectResponse.contentLength} bytes from $key")
3334

3435
// http://stackoverflow.com/questions/17782937/connectionpooltimeoutexception-when-iterating-objects-in-s3
3536
try {
@@ -41,10 +42,10 @@ trait S3 extends GuLogging {
4142
result.close()
4243
}
4344
} catch {
44-
case e: AmazonS3Exception if e.getStatusCode == 404 =>
45+
case e: NoSuchKeyException if e.statusCode == 404 =>
4546
log.warn(s"not found at ${objectId.s3Uri}")
4647
None
47-
case e: AmazonS3Exception =>
48+
case e: software.amazon.awssdk.services.s3.model.S3Exception =>
4849
logS3ExceptionWithDevHint(objectId, e)
4950
None
5051
case e: Exception =>

0 commit comments

Comments
 (0)