def withS3client[T]()

in magenta-lib/src/main/scala/magenta/tasks/AWS.scala [107:191]


  def withS3client[T](
      keyRing: KeyRing,
      region: Region,
      config: ClientOverrideConfiguration = AWS.clientConfiguration,
      resources: DeploymentResources
  )(block: S3Client => T): T =
    withResource(
      S3Client
        .builder()
        .region(region.awsRegion)
        .credentialsProvider(AWS.provider(keyRing, resources))
        .overrideConfiguration(config)
        .build()
    )(block)

  /** Check (and if necessary create) that an S3 bucket exists for the account
    * and region. Note that it is assumed that the clients and region provided
    * all match up - i.e. they are all configured to the same region and use the
    * same credentials.
    * @param prefix
    *   The prefix for the bucket. The resulting bucket will be
    *   <prefix>-<accountNumber>-<region>.
    * @param s3Client
    *   S3 client used to check and create the bucket and set a lifecycle policy
    *   if deleteAfterDays is set
    * @param stsClient
    *   The STS client that is used to retrieve the account number
    * @param region
    *   The region in which the bucket should be created
    * @param deleteAfterDays
    *   If set then if this bucket is created then a lifecycle policy rule is
    *   created to delete objects uploaded to this bucket after the number of
    *   days specified
    *
    * @return
    */
  def accountSpecificBucket(
      prefix: String,
      s3Client: S3Client,
      stsClient: StsClient,
      region: Region,
      reporter: DeployReporter,
      deleteAfterDays: Option[Int] = None
  ): String = {
    val accountNumber = STS.getAccountNumber(stsClient)
    val bucketName = s"$prefix-$accountNumber-${region.name}"
    if (!s3Client.listBuckets.buckets.asScala.exists(_.name == bucketName)) {
      reporter.info(
        s"Creating bucket for this account and region: $bucketName ${region.name}"
      )

      val createBucketRequest =
        CreateBucketRequest.builder().bucket(bucketName).build()
      s3Client.createBucket(createBucketRequest)

      deleteAfterDays.foreach { days =>
        val daysString = s"$days day${if (days == 1) "" else "s"}"
        reporter.info(
          s"Creating lifecycle rule on bucket that deletes objects after $daysString"
        )
        s3Client.putBucketLifecycleConfiguration(
          PutBucketLifecycleConfigurationRequest
            .builder()
            .bucket(bucketName)
            .lifecycleConfiguration(
              BucketLifecycleConfiguration
                .builder()
                .rules(
                  LifecycleRule
                    .builder()
                    .id(s"Rule to delete objects after $daysString")
                    .status(ExpirationStatus.ENABLED)
                    .expiration(
                      LifecycleExpiration.builder().days(days).build()
                    )
                    .build()
                )
                .build()
            )
            .build()
        )
      }
    }
    bucketName
  }