Skip to content

Commit

Permalink
perf: supress AWK SDK warnings
Browse files Browse the repository at this point in the history
see #28
  • Loading branch information
bogovicj committed Dec 12, 2024
1 parent f84541d commit 4b78487
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class AmazonS3Utils {
public static final Pattern AWS_ENDPOINT_PATTERN = Pattern.compile("^(.+\\.)?(s3\\..*amazonaws\\.com)", Pattern.CASE_INSENSITIVE);
public final static Pattern S3_SCHEME = Pattern.compile("s3", Pattern.CASE_INSENSITIVE);

private AmazonS3Utils() {
private static final String DISABLE_WARNING_KEY = "aws.java.v1.disableDeprecationAnnouncement";

private AmazonS3Utils() {
}

public static String getS3Bucket(final String uri) {
Expand Down Expand Up @@ -102,14 +103,30 @@ private static Regions parseRegion(String stringRegionFromUri) {

public static AWSCredentialsProvider getS3Credentials(final AWSCredentials s3Credentials, final boolean s3Anonymous) {

/*
* TODO necessary until we update to AWS SDK (2.x)
* see https://github.com/saalfeldlab/n5-aws-s3/issues/28
*/
final String initialDisableWarningPropertyValue = System.getProperty(DISABLE_WARNING_KEY);
if( initialDisableWarningPropertyValue == null)
System.setProperty(DISABLE_WARNING_KEY, "true");

if (s3Credentials != null) {
return new AWSStaticCredentialsProvider(s3Credentials);
final AWSStaticCredentialsProvider provider = new AWSStaticCredentialsProvider(s3Credentials);
resetDisableWarningValue(initialDisableWarningPropertyValue);
return provider;
} else {
// if not anonymous, try finding credentials
if (!s3Anonymous)
return new DefaultAWSCredentialsProviderChain();
if (!s3Anonymous) {
final DefaultAWSCredentialsProviderChain provider = new DefaultAWSCredentialsProviderChain();
resetDisableWarningValue(initialDisableWarningPropertyValue);
return provider;
}
else
{
resetDisableWarningValue(initialDisableWarningPropertyValue);
return null;
}
}
}

Expand Down Expand Up @@ -190,6 +207,15 @@ public static AmazonS3 createS3(
@Nullable final Regions region) {

final boolean isAmazon = endpointConfiguration == null || AmazonS3Utils.AWS_ENDPOINT_PATTERN.matcher(endpointConfiguration.getServiceEndpoint()).find();

/*
* TODO necessary until we update to AWS SDK (2.x)
* see https://github.com/saalfeldlab/n5-aws-s3/issues/28
*/
final String initialDisableWarningPropertyValue = System.getProperty(DISABLE_WARNING_KEY);
if( initialDisableWarningPropertyValue == null)
System.setProperty(DISABLE_WARNING_KEY, "true");

final AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();

if (!isAmazon)
Expand Down Expand Up @@ -220,12 +246,21 @@ else if (region != null)
// bucket not detected with anonymous credentials, try detecting credentials
// and return it even if it can't detect the bucket, since there's nothing else to do
builder.withCredentials(new DefaultAWSCredentialsProviderChain());
resetDisableWarningValue(initialDisableWarningPropertyValue);
return builder.build();
}
}

resetDisableWarningValue(initialDisableWarningPropertyValue);
return s3;
}

private static void resetDisableWarningValue(final String initialDisableWarningPropertyValue) {

if (initialDisableWarningPropertyValue == null)
System.clearProperty(DISABLE_WARNING_KEY);
}

private static boolean canListBucket(final AmazonS3 s3, final String bucket) {

final ListObjectsV2Request request = new ListObjectsV2Request();
Expand Down

0 comments on commit 4b78487

Please sign in to comment.