Allow configuration of AWS region

This commit is contained in:
Shane Kilkelly 2021-01-22 09:53:12 +00:00
parent 6d44851869
commit c520ecd70d
4 changed files with 34 additions and 10 deletions

View file

@ -19,7 +19,8 @@
"type": "s3",
"awsAccessKey": "asdf",
"awsSecret": "asdf",
"s3BucketName": "com.overleaf.testbucket"
"s3BucketName": "com.overleaf.testbucket",
"awsRegion": "us-east-1"
},
"swapJob": {
"minProjects": 50,

View file

@ -21,16 +21,29 @@ public class S3SwapStore implements SwapStore {
this(
cfg.getAwsAccessKey(),
cfg.getAwsSecret(),
cfg.getS3BucketName()
cfg.getS3BucketName(),
cfg.getAwsRegion()
);
}
S3SwapStore(
String accessKey,
String secret,
String bucketName
String bucketName,
String region
) {
s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret))).build();
String regionToUse = null;
if (region == null) {
regionToUse = "us-east-1";
} else {
regionToUse = region;
}
s3 = AmazonS3ClientBuilder
.standard()
.withRegion(regionToUse)
.withCredentials(
new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret))
).build();
this.bucketName = bucketName;
}

View file

@ -9,6 +9,7 @@ public class SwapStoreConfig {
"noop",
null,
null,
null,
null
);
@ -16,19 +17,22 @@ public class SwapStoreConfig {
private String awsAccessKey;
private String awsSecret;
private String s3BucketName;
private String awsRegion;
public SwapStoreConfig() {}
public SwapStoreConfig(
String awsAccessKey,
String awsSecret,
String s3BucketName
String s3BucketName,
String awsRegion
) {
this(
"s3",
awsAccessKey,
awsSecret,
s3BucketName
s3BucketName,
awsRegion
);
}
@ -36,12 +40,14 @@ public class SwapStoreConfig {
String type,
String awsAccessKey,
String awsSecret,
String s3BucketName
String s3BucketName,
String awsRegion
) {
this.type = type;
this.awsAccessKey = awsAccessKey;
this.awsSecret = awsSecret;
this.s3BucketName = s3BucketName;
this.awsRegion = awsRegion;
}
public String getType() {
@ -60,12 +66,15 @@ public class SwapStoreConfig {
return s3BucketName;
}
public String getAwsRegion() { return awsRegion; }
public SwapStoreConfig sanitisedCopy() {
return new SwapStoreConfig(
type,
awsAccessKey == null ? null : "<awsAccessKey>",
awsSecret == null ? null : "<awsSecret>",
s3BucketName
s3BucketName,
awsRegion
);
}

View file

@ -10,6 +10,7 @@ public class S3SwapStoreTest {
private static final String accessKey = null;
private static final String secret = null;
private static final String bucketName = "com.overleaf.testbucket";
private static final String region = "us-east-1";
private S3SwapStore s3;
@ -19,7 +20,7 @@ public class S3SwapStoreTest {
s3 = null;
return;
}
s3 = new S3SwapStore(accessKey, secret, bucketName);
s3 = new S3SwapStore(accessKey, secret, bucketName, region);
}
// @Ignore
@ -38,4 +39,4 @@ public class S3SwapStoreTest {
// assertArrayEquals(contents, IOUtils.toByteArray(down));
// }
}
}