Skip to content

Commit

Permalink
use separate regional S3 client for accessing the replica bucket duri…
Browse files Browse the repository at this point in the history
…ng projection
  • Loading branch information
twrichards committed Jul 31, 2023
1 parent 0f094ef commit fc9d426
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ object S3Ops {
// TODO: Make this region aware - i.e. RegionUtils.getRegion(region).getServiceEndpoint(AmazonS3.ENDPOINT_PREFIX)
val s3Endpoint = "s3.amazonaws.com"

def buildS3Client(config: CommonConfig, forceV2Sigs: Boolean = false, localstackAware: Boolean = true): AmazonS3 = {
def buildS3Client(config: CommonConfig, forceV2Sigs: Boolean = false, localstackAware: Boolean = true, region: String = "eu-west-1"): AmazonS3 = {

val clientConfig = new ClientConfiguration()
// Option to disable v4 signatures (https://github.com/aws/aws-sdk-java/issues/372) which is required by imgops
Expand All @@ -260,6 +260,6 @@ object S3Ops {
case _ => AmazonS3ClientBuilder.standard().withClientConfiguration(clientConfig)
}

config.withAWSCredentials(builder, localstackAware).build()
config.withAWSCredentials(builder, localstackAware).withRegion(region).build()
}
}
13 changes: 7 additions & 6 deletions image-loader/app/model/Projector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Projector {
import Uploader.toImageUploadOpsCfg

def apply(config: ImageLoaderConfig, imageOps: ImageOperations, processor: ImageProcessor, auth: Authentication)(implicit ec: ExecutionContext): Projector
= new Projector(toImageUploadOpsCfg(config), S3Ops.buildS3Client(config), imageOps, processor, auth)
= new Projector(toImageUploadOpsCfg(config), S3Ops.buildS3Client(config), S3Ops.buildS3Client(config, region = "us-west-1"), imageOps, processor, auth)
}

case class S3FileExtractedMetadata(
Expand Down Expand Up @@ -83,6 +83,7 @@ object S3FileExtractedMetadata {

class Projector(config: ImageUploadOpsCfg,
s3: AmazonS3,
replicaS3: AmazonS3,
imageOps: ImageOperations,
processor: ImageProcessor,
auth: Authentication) extends GridLogging {
Expand All @@ -95,11 +96,11 @@ class Projector(config: ImageUploadOpsCfg,
import ImageIngestOperations.fileKeyFromId
val s3Key = fileKeyFromId(imageId)

val bucket = config.maybeReplicaBucket match {
val (s3Client, bucket) = config.maybeReplicaBucket match {
case _ if s3.doesObjectExist(config.originalFileBucket, s3Key) =>
config.originalFileBucket
case Some(replicaBucket) if s3.doesObjectExist(replicaBucket, s3Key) =>
replicaBucket
(s3, config.originalFileBucket)
case Some(replicaBucket) if replicaS3.doesObjectExist(replicaBucket, s3Key) =>
(replicaS3, replicaBucket)
case _ =>
logger.error(
logMarker,
Expand All @@ -109,7 +110,7 @@ class Projector(config: ImageUploadOpsCfg,
}

val s3Source = Stopwatch(s"object exists, getting s3 object at s3://$bucket/$s3Key to perform Image projection"){
s3.getObject(bucket, s3Key)
s3Client.getObject(bucket, s3Key)
}(logMarker)

try {
Expand Down
4 changes: 1 addition & 3 deletions image-loader/test/scala/model/ProjectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class ProjectorTest extends AnyFreeSpec with Matchers with ScalaFutures with Moc

private val config = ImageUploadOpsCfg(new File("/tmp"), 256, 85d, Nil, "img-bucket", None, "thumb-bucket")

private val s3 = mock[AmazonS3]
private val auth = mock[Authentication]
private val projector = new Projector(config, s3, imageOperations, ImageProcessor.identity, auth)
private val projector = new Projector(config, mock[AmazonS3], mock[AmazonS3], imageOperations, ImageProcessor.identity, mock[Authentication])

// FIXME temporary ignored as test is not executable in CI/CD machine
// because graphic lib files like srgb.icc, cmyk.icc are in root directory instead of resources
Expand Down

0 comments on commit fc9d426

Please sign in to comment.