From f2901bd6886cb8a756713407097720c0bc9dfb12 Mon Sep 17 00:00:00 2001 From: John Bogovic Date: Wed, 5 Jun 2024 10:07:20 -0400 Subject: [PATCH] perf: skip s3 checks when creditials explicitly passed (#33) * perf: skip s3 checks when creditials explicitly passed * style: AmazonS3UtilsTest * test: AmazonS3UtilsTest uses explicit anonymous credentials --- .../saalfeldlab/n5/s3/AmazonS3Utils.java | 28 +++++++++--------- .../saalfeldlab/n5/s3/AmazonS3UtilsTest.java | 29 +++++++++++-------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java b/src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java index f9567a7..603b54c 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java @@ -1,5 +1,14 @@ package org.janelia.saalfeldlab.n5.s3; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.Nullable; + +import org.janelia.saalfeldlab.n5.N5Exception; + import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.AWSStaticCredentialsProvider; @@ -12,15 +21,6 @@ import com.amazonaws.services.s3.AmazonS3URI; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.ListObjectsV2Request; -import org.janelia.saalfeldlab.n5.N5Exception; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.annotation.Nullable; public class AmazonS3Utils { public static final Pattern AWS_ENDPOINT_PATTERN = Pattern.compile("^(.+\\.)?(s3\\..*amazonaws\\.com)", Pattern.CASE_INSENSITIVE); @@ -108,7 +108,7 @@ public static AWSCredentialsProvider getS3Credentials(final AWSCredentials s3Cre if (!s3Anonymous) return new DefaultAWSCredentialsProviderChain(); else - return new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()); + return null; } } @@ -170,7 +170,9 @@ public static AmazonS3 createS3( if (!isAmazon) builder.withPathStyleAccessEnabled(true); - if (credentialsProvider != null) + if (credentialsProvider == null) + builder.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())); + else builder.withCredentials(credentialsProvider); if (endpointConfiguration != null) @@ -180,9 +182,9 @@ else if (region != null) else builder.withRegion("us-east-1"); - AmazonS3 s3 = builder.build(); + final AmazonS3 s3 = builder.build(); // try to listBucket if we are anonymous, if we cannot, don't use anonymous. - if (credentialsProvider != null && AmazonS3Utils.areAnonymous(credentialsProvider)) { + if (credentialsProvider == null) { // I initially tried checking whether the bucket exists, but // that, apparently, returns even when the client does not have access diff --git a/src/test/java/org/janelia/saalfeldlab/n5/s3/AmazonS3UtilsTest.java b/src/test/java/org/janelia/saalfeldlab/n5/s3/AmazonS3UtilsTest.java index 0c20d95..f66afde 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/s3/AmazonS3UtilsTest.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/s3/AmazonS3UtilsTest.java @@ -8,38 +8,43 @@ import java.util.HashMap; import java.util.Map; -import com.google.common.collect.Maps; import org.junit.Test; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.AnonymousAWSCredentials; + public class AmazonS3UtilsTest { @Test public void testUriParsing() throws URISyntaxException { // dummy client - String[] prefixes = new String[]{ + final String[] prefixes = new String[]{ "s3://", "https://s3-eu-west-1.amazonaws.com/", "http://localhost:8001/", }; - String[] buckets = new String[]{ + final String[] buckets = new String[]{ "zarr-n5-demo", "static.wk.org"}; - String[] paths = new String[]{ + final String[] paths = new String[]{ "", "foo.zarr", "data/sample"}; - for (String prefix : prefixes) - for (String bucket : buckets) - for (String path : paths) { - String uriString = prefix + bucket + "/" + path; - URI uri = new URI(uriString); + for (final String prefix : prefixes) + for (final String bucket : buckets) + for (final String path : paths) { + final String uriString = prefix + bucket + "/" + path; + final URI uri = new URI(uriString); assertEquals("bucket from uri", bucket, AmazonS3Utils.getS3Bucket(uri)); assertEquals("key from uri", path, AmazonS3Utils.getS3Key(uri)); - AmazonS3Utils.createS3(uriString); + AmazonS3Utils.createS3(uriString, + null, + new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()), + null); } } @@ -87,9 +92,9 @@ public void getS3InfoTest() { keyToTests.put("a", onePartKeyNoSlashTests); keyToTests.put("a/b/c/d", multiPartKeyNoSlashTests); - for (Map.Entry tests : keyToTests.entrySet()) { + for (final Map.Entry tests : keyToTests.entrySet()) { final String expectedKey = tests.getKey(); - for (String uri : tests.getValue()) { + for (final String uri : tests.getValue()) { assertEquals(bucketName, AmazonS3Utils.getS3Bucket(uri)); assertEquals("Unexpected key for " + uri, expectedKey, AmazonS3Utils.getS3Key(uri)); }