diff --git a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java index 5aaa58b85..fd86657c2 100644 --- a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java +++ b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3InstallationService.java @@ -14,10 +14,12 @@ import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.ResponseTransformer; +import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.*; import java.io.IOException; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Optional; @@ -26,19 +28,38 @@ public class AmazonS3InstallationService implements InstallationService { private final String bucketName; - private AwsCredentialsProvider credentialsProvider; + + private final AwsCredentialsProvider credentialsProvider; + private final Region region; + private final URI endpointOverride; + private boolean historicalDataEnabled; public AmazonS3InstallationService(String bucketName) { + this(bucketName, DefaultCredentialsProvider.create(), null, null); + } + + public AmazonS3InstallationService(String bucketName, AwsCredentialsProvider credentialsProvider) { + this(bucketName, credentialsProvider, null, null); + } + + public AmazonS3InstallationService( + String bucketName, + AwsCredentialsProvider credentialsProvider, + Region region, + String endpointOverride + ) { this.bucketName = bucketName; + this.credentialsProvider = credentialsProvider; + this.region = (region != null || System.getenv("AWS_REGION") == null) ? region : Region.of(System.getenv("AWS_REGION")); + this.endpointOverride = (endpointOverride != null && !endpointOverride.isEmpty()) ? URI.create(endpointOverride) : null; } @Override public Initializer initializer() { return (app) -> { // The first access to S3 tends to be slow on AWS Lambda. - this.credentialsProvider = DefaultCredentialsProvider.create(); - AwsCredentials credentials = createCredentials(credentialsProvider); + AwsCredentials credentials = createCredentials(this.credentialsProvider); if (credentials == null || credentials.accessKeyId() == null) { throw new IllegalStateException("AWS credentials not found"); } @@ -304,7 +325,11 @@ protected AwsCredentials createCredentials(AwsCredentialsProvider provider) { } protected S3Client createS3Client() { - return S3Client.builder().credentialsProvider(this.credentialsProvider).build(); + return S3Client.builder() + .credentialsProvider(this.credentialsProvider) + .region(this.region) + .endpointOverride(this.endpointOverride) + .build(); } private String getInstallerKey(Installer i) { diff --git a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3OAuthStateService.java b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3OAuthStateService.java index 83760ea35..b18336a77 100644 --- a/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3OAuthStateService.java +++ b/bolt/src/main/java/com/slack/api/bolt/service/builtin/AmazonS3OAuthStateService.java @@ -9,9 +9,11 @@ import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.ResponseTransformer; +import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.*; +import java.net.URI; import java.nio.charset.StandardCharsets; /** @@ -23,18 +25,36 @@ public class AmazonS3OAuthStateService implements OAuthStateService { private final String bucketName; - private AwsCredentialsProvider credentialsProvider; + + private final AwsCredentialsProvider credentialsProvider; + private final Region region; + private final URI endpointOverride; public AmazonS3OAuthStateService(String bucketName) { + this(bucketName, DefaultCredentialsProvider.create()); + } + + public AmazonS3OAuthStateService(String bucketName, AwsCredentialsProvider credentialsProvider) { + this(bucketName, credentialsProvider, null, null); + } + + public AmazonS3OAuthStateService( + String bucketName, + AwsCredentialsProvider credentialsProvider, + Region region, + String endpointOverride + ) { this.bucketName = bucketName; + this.credentialsProvider = credentialsProvider; + this.region = (region != null || System.getenv("AWS_REGION") == null) ? region : Region.of(System.getenv("AWS_REGION")); + this.endpointOverride = (endpointOverride != null && !endpointOverride.isEmpty()) ? URI.create(endpointOverride) : null; } @Override public Initializer initializer() { return (app) -> { // The first access to S3 tends to be slow on AWS Lambda. - this.credentialsProvider = DefaultCredentialsProvider.create(); - AwsCredentials credentials = createCredentials(credentialsProvider); + AwsCredentials credentials = createCredentials(this.credentialsProvider); if (credentials == null || credentials.accessKeyId() == null) { throw new IllegalStateException("AWS credentials not found"); } @@ -103,7 +123,11 @@ protected AwsCredentials createCredentials(AwsCredentialsProvider provider) { } protected S3Client createS3Client() { - return S3Client.builder().credentialsProvider(this.credentialsProvider).build(); + return S3Client.builder() + .credentialsProvider(this.credentialsProvider) + .region(this.region) + .endpointOverride(this.endpointOverride) + .build(); } private String getKey(String state) {