Skip to content

Commit

Permalink
fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
waziqi89 committed Mar 6, 2024
1 parent 5dcc375 commit 6c08737
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/main/java/com/yelp/nrtsearch/server/module/S3Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.amazonaws.retry.RetryPolicy;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
Expand All @@ -51,24 +52,34 @@ protected AmazonS3 providesAmazonS3(LuceneServerConfiguration luceneServerConfig
awsCredentialsProvider = new ProfileCredentialsProvider(profilesConfigFile, "default");
}
final boolean globalBucketAccess = luceneServerConfiguration.getEnableGlobalBucketAccess();
AmazonS3 s3ClientInterim =
AmazonS3ClientBuilder.standard()
.withCredentials(awsCredentialsProvider)
.withForceGlobalBucketAccessEnabled(globalBucketAccess)
.build();
String region = s3ClientInterim.getBucketLocation(luceneServerConfiguration.getBucketName());
// In useast-1, the region is returned as "US" which is an equivalent to "us-east-1"
// https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/Region.html#US_Standard
// However, this causes an UnknownHostException so we override it to the full region name
if (region.equals("US")) {
region = "us-east-1";
}
String serviceEndpoint = String.format("s3.%s.amazonaws.com", region);
logger.info(String.format("S3 ServiceEndpoint: %s", serviceEndpoint));

AmazonS3ClientBuilder clientBuilder =
AmazonS3ClientBuilder.standard()
.withCredentials(awsCredentialsProvider)
.withEndpointConfiguration(new EndpointConfiguration(serviceEndpoint, region));
.withForceGlobalBucketAccessEnabled(globalBucketAccess);
try {
AmazonS3 s3ClientInterim =
AmazonS3ClientBuilder.standard()
.withCredentials(awsCredentialsProvider)
.withForceGlobalBucketAccessEnabled(globalBucketAccess)
.build();
String region = s3ClientInterim.getBucketLocation(luceneServerConfiguration.getBucketName());
// In useast-1, the region is returned as "US" which is an equivalent to "us-east-1"
// https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/Region.html#US_Standard
// However, this causes an UnknownHostException so we override it to the full region name
if (region.equals("US")) {
region = "us-east-1";
}
String serviceEndpoint = String.format("s3.%s.amazonaws.com", region);
logger.info(String.format("S3 ServiceEndpoint: %s", serviceEndpoint));
clientBuilder.withEndpointConfiguration(new EndpointConfiguration(serviceEndpoint, region));
} catch (AmazonS3Exception amazonS3Exception) {
logger.warn(
"failed to get the location of S3 bucket: "
+ luceneServerConfiguration.getBucketName()
+ ". This could be caused by invalid credentials or invalid bucket name.",
amazonS3Exception);
}

int maxRetries = luceneServerConfiguration.getMaxS3ClientRetries();
if (maxRetries > 0) {
Expand All @@ -83,9 +94,6 @@ protected AmazonS3 providesAmazonS3(LuceneServerConfiguration luceneServerConfig
clientBuilder.setClientConfiguration(clientConfiguration);
}

if (globalBucketAccess) {
clientBuilder.enableForceGlobalBucketAccess();
}
return clientBuilder.build();
}
}

0 comments on commit 6c08737

Please sign in to comment.